gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:42:46 24 4
gpt4 key购买 nike

有人请... #$%^ 请看看这个。几天通过调试器,使用带有 jpeg 表示的 setData。使用 ios4 Assets 库设置文件,尝试新的 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]);
}

帮助!溺水……

最佳答案

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

世界都会好起来的,我要去买点吃的。干杯!

关于ios ASIFormDataRequest 报告来自 PHP 上传的无效文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7518923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com