0

当我尝试将图像上传到 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。

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

4

2 回答 2

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 中将其转换回图像。希望这可以帮助。

于 2013-01-11T06:45:35.543 回答
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];
}
于 2013-01-11T06:54:51.257 回答