问题描述
我从我的iPhone应用程式使用FTP上传档案时发生问题。
I am having a problem uploading files with FTP from my iPhone app.
我正在传送 HTTP POST
请求我的webservice所有必要的参数。我发送两个文件,一个是图像,第二个是音频。正在上传图片,但音频不是。
当我跟踪我的web服务,它只显示图像字段作为上传的文件。
I am sending an HTTP POST
request to my webservice with all the necessary parameters. I am sending two files, one is an image, and the second is audio. The image is being uploaded, but the audio isn't.When I trace my web service it only shows the image field as an uploaded file. It's not showing that the audio is also there.
我的代码是:
NSString *urlString = [NSString stringWithFormat:ADD_LEAD_API_URL];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// change type to POST (default is GET)
[postRequest setHTTPMethod:@"POST"];
// just some random text that will never occur in the body
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
// set header
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
// create data
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[appDelegate.objCurrentUser.userId dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// message part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"firstName\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[firstName dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
/*** My rest of string parameters are successfully added to request ***/
// media part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imageUrl\"; filename=\"dummy.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// get the image data from main bundle directly into NSData object
UIImage *img= leadImage;
NSData *imageData= UIImageJPEGRepresentation(img,90);
[postBody appendData:imageData];
// Image is being uploaded
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"recordSoundUrl\"; filename=\"%@\"\r\n", self.recordingPath] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: application/octet-stream\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSData *soundData = [NSData dataWithContentsOfFile:[appDelegate.documentDir stringByAppendingPathComponent:self.recordingPath]];
[postBody appendData:soundData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add body to post
[postRequest setHTTPBody:postBody];
// Asynch request
NSURLConnection *conn = [NSURLConnection connectionWithRequest:postRequest delegate:self];
如果我不上传图片,那么它需要音频。所以我能够只发送一个文件的请求。
If I don't upload the image, then it takes the audio. So I am able to send only one file with the request.Please help me and tell me if I am wrong anywhere.
请提前感谢。
推荐答案
您是否使用的 ASINetworkQueue
发送多个文件。
Have you looked at using ASIHTTPRequest's ASINetworkQueue
to send multiple files.
UPDATE:如下所示,ASIHTTPRequest不再维护。使用此框架时要小心。其他选项包括或。
UPDATE: As per comment below, ASIHTTPRequest is no longer maintained. Use caution with this framework. Other options are MKNetworkKit or AFNetworking.
这篇关于使用http POST上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!