ios ASIFormDataRequest报告PHP上传的无效文件

时间:2011-09-22 17:25:52

标签: ios ios4 asiformdatarequest

有人请...#$%^请看看这个。使用带有jpeg表示的setData,遍历调试器的日子。使用ios4资产库设置文件,尝试新的PHP脚本,删除asiHTTPrequest文件并确定我有新的文件。仍然没有......一半的代码已经从这里或网上其他地方的例子中汇总而成。

这里的目标是简单地从相机胶卷中选择一张照片,上传它,看起来很简单,我有一个不同的PHP脚本,从桌面上工作得很好,从这里抓了一个,因为它更加简洁和它也适用于桌面。

所以完成图像拾取的覆盖

- (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];
}

然后是上传方法。

-(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];

}

然后是完成/失败选择器

-(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]);
}

帮助!溺水...

1 个答案:

答案 0 :(得分:0)

这是PHP的一个问题。

move_uploaded_file($_FILES["file"]["tmp_name"]
问题就在于此。

如果你看这个

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

它正在改变POST,所以标准......

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

需要

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

添加

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

到PHP让我看到..

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

 )

中定义的选择器中的

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

我将从这里做的是将PHP恢复到之前的位置

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

并将setFile调用更改为

[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"file"];
一切都会与世界很好,我会得到一些食物。干杯!