2013-01-11 96 views
0

我嘗試上傳圖像到php腳本時,使用ASIHTTPRequest庫時出現了一個奇怪的問題。我已經正確實現了ASIHTTPRequest,因爲php腳本從iPhone模擬器接收POST數據,但僅用於測試集中的一些圖像。還有其他圖像不通過POST。ASIHTTPRequest圖像上傳不能用於某些圖像

所有圖像都是從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。

有什麼建議嗎?我將不勝感激!

回答

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]; 
} 
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我把它轉換回爲一個圖像。希望這可以幫助。