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

发布于 2024-12-06 04:12:36 字数 4322 浏览 1 评论 0原文

请有人... #$%^ 请看一下这个。使用 setData 和 jpeg 表示来浏览调试器。使用 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]);
}

帮助!溺水...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-12-13 04:12:36

这是 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"];

all 将会很好,我会得到一些食物。干杯!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文