开发者

ios ASIFormDataRequest reporting invalid file from PHP upload

Somebody please... #$%^ please take a look at this. days walking through the debugger, using setData with a jpeg representation. set file using ios4 asset library, trying a new PHP script, deleting the asiHTTPrequest files and making damn sure I have the new ones. Still nothing... Half the code has been put together from examples here or elsewhere on the web.

The goal here, is to simply pick a photo from the camera roll, and upload it, seems pretty easy, I had a different PHP script that was working fine from the desktop and nabbed one from here because it's much more concise and it works from the desktop as well.

so the override for finishing image picking

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;

// dealing with a still image
if(CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo){

    editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];

    originalImage = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];

    /*
    if(editedImage){
        imageToSave = editedImage;
    } else {
        imageToSave = originalImage;
    }
    */
    chosenImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

    //_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(originalImage, 0.0)];
    //_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(chosenImage.image, 0.0)];

    UIImage *im = [info objectForKey:@"UIImagePickerControllerOriginalImage"] ;
    UIGraphicsBeginImageContext(CGSizeMake(320,480)); 
    [im drawInRect:CGRectMake(0, 0,320,480)];
    _resizedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();
    _imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(_resizedImage, 0.0)];

    }
    [picker release];
}

then the upload method.

-(void)uploadPhoto
{
//NSLog(@"image path inside uploadPhoto --> %@"开发者_高级运维, _imagePath);
NSLog(@"uploadPhoto");


//NSLog(@"%@", imageData);

//_imageData = _imageData;

NSString *unescapedURL = @"http://dev.xxxx.com/upload.php";

NSString * escapedURL =
(NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                    NULL,
                                                    (CFStringRef)unescapedURL,
                                                    NULL,
                                                    (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                    kCFStringEncodingUTF8 );


NSURL *url = [NSURL URLWithString:unescapedURL];
//NSURL *url = [NSURL URLWithString:unescapedURL];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setRequestMethod:@"POST"];
//[request setStringEncoding:NSUTF8StringEncoding];
//[request addPostValue:@"submit" forKey:@"Submit"];
//[request setPostValue:@"Submit" forKey:@"Submit"];
[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
//[request setFile:_imagePath forKey:@"photo"];
//[request setFile:_imagePath withFileName:@"image5.png" andContentType:@"image/png" forKey:@"photo"];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setTimeOutSeconds:500];
[request startAsynchronous];

NSError *error = nil;
NSString *theString = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
if( theString )
{
    NSLog(@"Text=%@", theString);
}
else 
{
    NSLog(@"Error = %@", error);
    NSString *localized = [error localizedDescription];
    NSString *localizedFail = [error localizedFailureReason] ? [error localizedFailureReason] : NSLocalizedString(@"not it", nil);
    NSLog(@"localized error--> %@", localized);
    NSLog(@"localizedFail--> %@", localizedFail);

}

[escapedURL release];

}

then the finish/fail selectors

-(void)requestFinished:(ASIFormDataRequest *)request
{
    NSLog(@"requestFinished");
    NSString *respondingString = [request responseString];
    NSLog(@"response string--> %@", respondingString);

    NSData *responseData = [request responseData];
    NSLog(@"%@", responseData);
}

-(void)requestFailed:(ASIFormDataRequest *)request
{
    NSLog(@"requestFailed");
    //NSError *error = [request error];
    //NSLog(@"%@", [error description]);
}

Help! Drowning...


It was a problem with the PHP.

move_uploaded_file($_FILES["file"]["tmp_name"]

was the issue.

if you look at this

[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

It's changing the POST so the standard...

move_uploaded_file($_FILES["file"]["tmp_name"]

needs to be

move_uploaded_file($_FILES["photo"]["tmp_name"]

adding

error_reporting(E_ALL);
ini_set("display_errors", 1); 
print_r($_FILES);

to the PHP allowed me to see..

response string--> Array
(
    [photo] => Array
        (
            [name] => image4.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpCSXJgl
            [error] => 0
            [size] => 150854
        )

 )

in the selector defined by...

[request setDidFinishSelector:@selector(requestFinished:)];

What I'll do from here is revert the PHP to where it was before

move_uploaded_file($_FILES["file"]["tmp_name"]

and change the setFile call to

[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"file"];

all will be well with the world and I'm going to get some food. cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜