【发布时间】:2011-04-19 12:53:43
【问题描述】:
我在使用 FTP 从我的 iPhone 应用程序上传文件时遇到问题。
我正在向我的网络服务发送一个HTTP POST 请求,其中包含所有必要的参数。我正在发送两个文件,一个是图像,第二个是音频。图片正在上传,但音频没有。
当我跟踪我的网络服务时,它只将图像字段显示为上传的文件。它没有显示音频也在那里。
我的代码是:
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];
如果我不上传图片,那么它会使用音频。所以我只能发送一个带有请求的文件。 请帮助我,如果我在任何地方错了,请告诉我。
提前致谢。
【问题讨论】:
-
嗨卡皮尔,我面临同样的问题。你能帮帮我吗?
-
你在做什么?您是否尝试使用我的方式上传多个文件?如果是这样,则在添加图像数据后使用以下行:
[postBody appendData:imageData];替换:[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];与[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];... 希望这会有所帮助 -
如果这不起作用,请告诉我。我随时为您提供帮助
-
Kapil 我尝试了您的解决方案,但它对我不起作用,所以我将使用 ASIHTTPRequest。无论如何,谢谢。
-
嗨,我正在使用这种方法在
facebook上发帖。这适用于单张照片。但是我想上传单张照片的数量,即使我附加了2-3张图片,这种方法也只会上传一张图片。有没有更好的方法在 iOS 的 facebook 上多次上传?
标签: objective-c cocoa-touch ftp http-post