使用http POST上传多个文件
|
我从iPhone应用程序使用FTP上传文件时遇到问题。
我正在向我的Web服务发送带有所有必要参数的“ 0”请求。我正在发送两个文件,一个是图像,第二个是音频。图片正在上传,但音频未上传。
当我跟踪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];
如果我不上传图像,则它将接收音频。因此,我只能随请求发送一个文件。
请帮助我,告诉我任何地方我错了。
提前致谢。
没有找到相关结果
已邀请:
3 个回复
马口
发送多个文件。 更新:根据下面的注释,不再维护ASIHTTPRequest。在此框架上要格外小心。其他选项是MKNetworkKit或AFNetworking。
伐教爸
现在,您可以通过任何方式分派它:
FileUpload类相当简单:它从url /文件名构造,将此名称作为方法提供,并将二进制内容加载到NSData *中。为了清楚起见,我没有列出它,但是如果需要,我会添加。
孝箱捆讨
并使用它
代替这个