使用http POST上传多个文件

问题描述:

我在从iPhone应用上传FTP文件时出现问题。使用http POST上传多个文件

我发送一个HTTP POST请求到我的webservice所有必要的参数。我发送两个文件,一个是图像,另一个是音频。图像正在上传,但音频不是。 当我跟踪我的Web服务时,它只显示图像字段作为上传的文件。这并不表明音频也在那里。

我的代码是:

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]; 

如果我不上传图片,那么它需要的音频。所以我只能发送一个包含请求的文件。 请帮我告诉我,如果我在任何地方都是错的。

在此先感谢。

+0

嗨Kapil,我面临同样的问题。你能帮我解决这个问题吗? – 2012-02-02 12:30:00

+0

你在做什么?您是否尝试使用我所做的方式上传多个文件?如果是这样,在添加图像数据后添加以下代码:'[postBody appendData:imageData];'replace:'[postBody appendData:[[NSString stringWithFormat:@“ - %@ \ r \ n”,stringBoundary] dataUsingEncoding:NSUTF8StringEncoding] ];'用'[postBody appendData:[[NSString stringWithFormat:@“\ r \ n - %@ \ r \ n”,stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];'...希望这可以帮助 – 2012-02-03 13:48:50

+0

这不工作..我总是在这里帮助 – 2012-02-03 13:49:26

你看过使用ASIHTTPRequestASINetworkQueue发送多个文件。

更新:根据以下评论,ASIHTTPRequest不再维护。请谨慎使用此框架。其他选项是MKNetworkKitAFNetworking

+0

是的,这也是一个不错的选择,但我并没有改变我的整个代码。在添加图像数据后,通过添加'“\ r \ n”'完成相同的代码。感谢任何方式 – 2011-04-28 07:17:57

+1

请注意,ASIHTTPRequest在发布和现在之间已被弃用。任何人阅读本文时都应该谨慎使用。 – 2012-10-04 23:42:30

+1

谢谢@StevenFisher:更新了我的回答 – 2012-10-30 09:25:23

您可能在设置边界时遇到问题。

以此为界

NSString boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 

,并使用此

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

,而不是这个

[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
+1

这是近一年的旧帖子。如果你在我自己的问题上看到评论,我已经提到我是如何解决我的问题的,这与你的答案相同。我已经使用您所说的相同方式解决了这个问题(您可以看到我的评论)。无论如何谢谢你试图帮助我.. – 2012-05-28 11:18:29

+1

你应该接受最正确的答案;你应该能够移动你接受的答案。 – 2012-10-04 23:41:21

这是相当简单的。只是形成多部分机构。假设你已经在“PARAMS”字典文件上传和定期PARAMS:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: urlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:54.0]; 

NSMutableData* body = [NSMutableData data]; 

NSString* boundary = [NSString randomStringWithLength:64]; 
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"]; 
NSData *boundaryData = [[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]; 

[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 
    [body appendData:boundaryData]; 
    // I use special simple class to distinguish file params 
    if([obj isKindOfClass:[FileUpload class]]) { 
     // File upload 
     [body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", key, [obj localName]] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData: [obj loadData]]; // It just return NSData with loaded file in it 
     [body appendData: [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 
    else { 
     // Regular param 
     [body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n", key, obj] dataUsingEncoding:NSUTF8StringEncoding]] 
    } 
}]; 
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:body]; 

现在,你可以派遣以任何方式:

[NSURLConnection sendAsynchronousRequest:request 
            queue:queueOfYourChoice 
         completionHandler:^(NSURLResponse *response, NSData *rdata, NSError *error) 

的FileUpload类是相当容易:它从构建url/filename,将该名称作为方法提供,并将二进制内容加载到NSData *中。我没有列出清楚,但如果需要,我会补充。

+0

这位先生的参数是什么? – 2014-08-11 13:20:46

+0

这是一个包含您的请求参数的字典。 – sergeych 2014-09-18 20:52:17