ASIHTTPRequest图像上传不适用于某些图像

时间:2013-01-11 03:37:59

标签: iphone xcode image asihttprequest

当我尝试将图像上传到php脚本时,我有一个使用ASIHTTPRequest库的奇怪问题。我已经正确实现了ASIHTTPRequest,因为php脚本确实从iphone模拟器接收POST数据,但仅用于我的测试集中的一些图像。还有其他图像无法通过POST。

所有图片均来自Facebook,并且是jpg或png格式。我已经在两种类型的图像上测试了我的代码,但这并不重要,因为我在我的iphone应用程序中使用PNGRepresentation方法将图像转换为NSData。我还测试了图像的大小,这不是问题(范围从600x600到1200x1200)。

打破ASIHTTPRequest的图像对我来说似乎并不特别,我无法识别错误。以下是我的一些实现:

iPhone实施:

[RegisterRequest setData:profilePicturePNG withFileName:filename andContentType:@"image/png" forKey:@"profilePicture"];
[RegisterRequest addRequestHeader:@"Content-Type" value:@"image/png"];
[RegisterRequest setDelegate:self];
[RegisterRequest startAsynchronous];

PHP实施:

echo "Upload: " . $_FILES["profilePicture"]["name"] . "<br>";
echo "Type: " . $_FILES["profilePicture"]["type"] . "<br>";
echo "Size: " . ($_FILES["profilePicture"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["profilePicture"]["tmp_name"] . "<br>";

在此测试中,PHP实现应该回显文件属性。正如我之前所说,对于大多数图像,我确实得到回声。但是有一些图像的名称和类型没有通过,大小报告为0kb。

有什么建议吗?我将不胜感激!

2 个答案:

答案 0 :(得分:1)

我想图像上传你不应该使用ASIHTTPRequest

 NSString *requestStr = [yourPHPSCriptURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
responseString = nil;

NSURL *requestUrl = [[NSURL alloc] initWithString:requestStr];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:requestUrl];

[request setPostFormat:ASIMultipartFormDataPostFormat];
[request addPostValue:@".png" forKey:image_content_type];  //Use this if you want//
[request setShouldAttemptPersistentConnection:YES];
[request setData:yourPhotoData withFileName:@"user_image_byte.png" andContentType:@"image/png" forKey:@"user_image_byte"];

[request startSynchronous];

我将我的图像作为字节流发送,稍后在asp.net中将其转换回图像。希望这会有所帮助。

答案 1 :(得分:1)

对于您的信息,ASIHTTPRequest已被弃用,但您可以在此处使用AFNetworking,您可以参考此处。 https://github.com/AFNetworking/AFNetworking以及此示例

-(IBAction)uploadButtonClicked:(id)sender{

NSData *imageToUpload = UIImageJPEGRepresentation(mainImageView.image, 90);

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.THESERVER.com"]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/PROJECT/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: imageToUpload name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: [%@]",response);
[MBProgressHUD hideHUDForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
if([operation.response statusCode] == 403){
    NSLog(@"Upload Failed");
    return;
}
NSLog(@"error: %@", [operation error]);

}];

[operation start];
}