0
votes

I have a strange issue using the ASIHTTPRequest library when I attempt to upload images to a php script. I have implemented ASIHTTPRequest correctly, as in the php script does receive the POST data from the iphone simulator, but only for some of the images in my testing set. There are other images that don't pass through the POST.

All images were retrieved from Facebook, and are either jpg or png format. I have tested my code on both types of images, though it shouldn't matter because I use the PNGRepresentation method in my iphone application to convert the image to NSData. I have also tested size of an image, and this is not an issue (ranging from 600x600 to 1200x1200).

The images that break the ASIHTTPRequest don't seem special at all to me, and I am having trouble identifying the bug. Below is some of my implementation:

iPhone Implementation:

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

PHP Implementation:

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>";

In this test, the PHP implementation should echo the file properties. As I said earlier, for most images I do get an echo back. But there are some images where name and type don't go through and size is reported as 0kb.

Any suggestions? I would greatly appreciate it!

2

2 Answers

1
votes

For your info, ASIHTTPRequest is being deprecated, but you can use AFNetworking here you can reference from this. https://github.com/AFNetworking/AFNetworking and also this example

-(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
votes

I guess for image uploading you should not use 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];

I send my image as byte stream and later in asp.net I convert it back as a image. Hope this helps.