objective-c – 使用http POST上传多个文件

前端之家收集整理的这篇文章主要介绍了objective-c – 使用http POST上传多个文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在从iPhone应用程序上使用FTP上传文件时遇到问题.

我正在向我的webservice发送一个带有所有必要参数的HTTP POST请求.我发送两个文件,一个是图像,第二个是音频.图像正在上传,但音频不上传.
当我跟踪我的Web服务时,它只显示图像字段作为上传文件.它没有表明音频也存在.

我的代码是:

  1. NSString *urlString = [NSString stringWithFormat:ADD_LEAD_API_URL];
  2.  
  3. NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
  4.  
  5.  
  6.  
  7. // change type to POST (default is GET)
  8. [postRequest setHTTPMethod:@"POST"];
  9.  
  10.  
  11. // just some random text that will never occur in the body
  12. NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
  13.  
  14. // header value
  15. NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
  16.  
  17. // set header
  18. [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
  19.  
  20. // create data
  21. NSMutableData *postBody = [NSMutableData data];
  22.  
  23.  
  24.  
  25. [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  26. [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  27. [postBody appendData:[appDelegate.objCurrentUser.userId dataUsingEncoding:NSUTF8StringEncoding]];
  28. [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  29.  
  30.  
  31. // message part
  32. [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  33. [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"firstName\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  34. [postBody appendData:[firstName dataUsingEncoding:NSUTF8StringEncoding]];
  35. [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  36.  
  37. /*** My rest of string parameters are successfully added to request ***/
  38.  
  39.  
  40. // media part
  41. [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  42. [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imageUrl\"; filename=\"dummy.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  43. [postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  44. [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  45. // get the image data from main bundle directly into NSData object
  46.  
  47. UIImage *img= leadImage;
  48. NSData *imageData= UIImageJPEGRepresentation(img,90);
  49.  
  50. [postBody appendData:imageData];
  51.  
  52. // Image is being uploaded
  53.  
  54. [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  55. [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"recordSoundUrl\"; filename=\"%@\"\r\n",self.recordingPath] dataUsingEncoding:NSUTF8StringEncoding]];
  56. [postBody appendData:[@"Content-Type: application/octet-stream\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  57. [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  58.  
  59.  
  60. NSData *soundData = [NSData dataWithContentsOfFile:[appDelegate.documentDir stringByAppendingPathComponent:self.recordingPath]];
  61. [postBody appendData:soundData];
  62. [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  63.  
  64. // final boundary
  65. [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  66.  
  67. // add body to post
  68. [postRequest setHTTPBody:postBody];
  69.  
  70. // Asynch request
  71. NSURLConnection *conn = [NSURLConnection connectionWithRequest:postRequest delegate:self];

如果我不上传图像,则需要音频.所以我只能发送一个带有请求的文件.
请帮帮我,告诉我在任何地方我都错了.

提前致谢.

解决方法

你有没有看过使用 ASIHTTPRequest的ASINetworkQueue发送多个文件.

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

猜你在找的C&C++相关文章