3

我正在发送一张构建 HTTP POST 的图片,如下所示:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];


NSString *boundary = @"801f6dd4cd17";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=------------------------------%@", boundary];


NSMutableDictionary *headerFieldsDict = [[ NSMutableDictionary alloc] init];
[ headerFieldsDict setObject:contentType forKey:@"Content-Type"];

[request setAllHTTPHeaderFields:headerFieldsDict];

NSMutableData *body = [NSMutableData data];

//---------- FIRST IMG
UIImage* imgLittle = [realImgButton imageForState:UIControlStateNormal];//thumImg.image;
NSData *imageData = UIImageJPEGRepresentation(imgLittle, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"------------------------------%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"img.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

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

[request setHTTPBody:body];

但是服务器拒绝上传图像(它给出了一个软件错误,似乎 HTTP 帖子没有很好地解析),我关于服务器的唯一信息是这个客户端代码在 php 中运行良好:

$data['file'] = "@".$file;
$header[] = "Expect: ";
print_r($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );

//curl_setopt($ch, CURLOPT_URL, 'http://xxxxxx:yyy/');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
$url = strip_tags($xml->dict->string[3]->asXML());
return $url;

有任何想法吗?或者我在 obj-c 代码上哪里错了?我尝试分析生成的 HTTP post 语句,它似乎与 php 代码生成的语句相同。

4

4 回答 4

0

您可以使用ASIHTTPRequest框架:我的项目之一是使用它在服务器上上传图像:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:yourURL];
[request addPostValue:NSString  forKey:@"KEY"];
[request addPostValue:NSString forKey:@"KEY"];
... any post value you need

[request addData:image withFileName:[name of the file]
    andContentType:@"image/jpeg" forKey:@"KEY"];

希望这有帮助:)

于 2012-10-15T19:09:29.070 回答
0

你发布的代码看起来很熟悉,所以我检查了一个我一直在发布图像的旧应用程序,我们显然是从同一来源获得的代码(我不记得了)。我的几乎是一样的,除了边界字符串,我去掉了多余的字符串[NSString stringWithFormat:]

[request setHTTPMethod:@"POST"];

NSString *boundary = @"0xKhTmLbOuNdArY"; 

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

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

[body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:myImageData];
[body appendData:[[NSString stringWithFormat:@"r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

这段代码对我来说很好,所以假设你的图像数据没有问题,你的边界可能就是查看的地方。我的图像数据也是从UIImageJPEGRepresentation

于 2012-10-18T18:31:14.200 回答
0

我不确定您的代码有什么问题,但我有两个想法:

  1. 我认为您可能需要\r\n在 结尾处再添加一个Content-Disposition,如下所示:

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"img.jpg\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
  2. 使用UIImageJPEGRepresentation并将您的内容类型设置为application/octet-stream可能是问题所在。[NSData dataWithContentsOfFile:]可能更适合application/octet-stream并且image/jpeg可能更适合UIImageJPEGRepresentation. 这只是一个猜测。

如果你真的被难住了,你可以使用AFNetworking(它比 ASIHTTPRequest 更新且重量更轻)。或者您至少可以查看他们的代码以了解他们如何处理文件上传。

于 2012-10-17T15:51:40.777 回答
0

你可能想去-addValue:forHTTPHeaderField:而不是这样做:

NSMutableDictionary *headerFieldsDict = [[ NSMutableDictionary alloc] init];
[ headerFieldsDict setObject:contentType forKey:@"Content-Type"];

[request setAllHTTPHeaderFields:headerFieldsDict];

您真正需要做的就是在请求中添加一个标头,但您实际上是在使用上面的代码清除所有现有的标头。

于 2012-10-10T22:24:51.313 回答