ios ASIFormDataRequest 报告来自 PHP 上传的无效文件
请有人... #$%^ 请看一下这个。使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 PHP 的问题。
是问题所在。
如果你看看这个
它正在改变POST,所以标准...
需要添加
到
PHP中让我可以看到..
在由...定义的选择器中...
我从这里要做的就是将PHP恢复到它的位置之前
将 setFile 调用更改为
all 将会很好,我会得到一些食物。干杯!
It was a problem with the PHP.
was the issue.
if you look at this
It's changing the POST so the standard...
needs to be
adding
to the PHP allowed me to see..
in the selector defined by...
What I'll do from here is revert the PHP to where it was before
and change the setFile call to
all will be well with the world and I'm going to get some food. cheers!