新手有使用ASIHTTPRequest上传文件的问题 [英] Newbie having issues with uploading a file using ASIHTTPRequest
问题描述
我真的,真的,真的使用一些帮助。我已经看了无处不在一些详细的文档(对于虚拟)使用ASIFormDataRequest的照片上传。
这是什么(在相关的部分)我有,虽然我知道我可能会离开。
Camera.h
#import< UIKit / UIKit.h>
@class ASIFormDataRequest;
...
Camera.m
#importCamera.h
#importASIHTTPRequest.h
#importASIFormDataRequest.h
...
- (void)uploadImage:(NSData *)imageData filename:(NSString *)filename {
NSURL * url = [NSURL URLWithString:@http: //moosesightings.com/test.php];
ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url];
//在磁盘上上传文件
[请求setData:imageData withFileName:filename andContentType:@image / jpegforKey:@userfile];
[request setDelegate:self];
[request startAsynchronous];
}
...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
[picker.view removeFromSuperview];
//从信息字典访问未裁剪的图像
UIImage * image = [info objectForKey:@UIImagePickerControllerOriginalImage];
NSString * imageName = @uploaded.jpg;
//保存图片
// UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo :),nil);
[self uploadImage:UIImageJPEGRepresentation(imageView.image,1.0)filename:imageName];
captionControls.hidden = NO;
[textView becomeFirstResponder];
[picker release];
}
这是我收到的脚本。
$ dir_dest ='uploads /';
ob_start();
print_r($ _ FILES);
$ test = ob_get_contents();
ob_end_clean();
$ db-> insert('connecttest',array('ts'=> date('Ymd H:i:s'),'tempfield'=> $ test)) - > execute );
这是我得到的结果
Array
(
[null]] => Array
(
[name] =>(null)
[type] =>(null)
[tmp_name] => C:\Windows \Temp\phpCFF0.tmp
[error] => 0
[size] => 0
)
)
(
我遇到了类似的问题,使用
调整图片大小UIImage * im = [info objectForKey:@UIImagePickerControllerOriginalImage];
pre>
UIGraphicsBeginImageContext(CGSizeMake(320,480));
[im drawInRect:CGRectMake 0,320,480)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imageData = UIImageJPEGRepresentation(newImage,0.5);
,它开始工作,捕获的图像的高度和宽度将超过2000x2000,所以减少它,并尝试。
[request setData:imageData withFileName:@photo.jpgandContentType:@image / jpegforKey:@file ];
这是php脚本。
$ filename =uploaded;
$ target_path =uploads /;
$ target_path = $ target_path。$ filename。。jpg;
if(move_uploaded_file($ _ FILES ['file'] ['tmp_name'],$ target_path)){
echouploaded a image;
} else {
echo上传文件时出错,请重试!
}
I could really, really, really use some help. I have looked everywhere on some detailed documentation (for dummies) on using ASIFormDataRequest for photo uploads. Can someone through me a life line here?
Here is what (in relevance pieces) I have though I know I may be way off.
Camera.h
#import <UIKit/UIKit.h> @class ASIFormDataRequest; ...
Camera.m
#import "Camera.h" #import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h" ... - (void)uploadImage:(NSData *)imageData filename:(NSString *)filename{ NSURL *url = [NSURL URLWithString:@"http://moosesightings.com/test.php"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; // Upload a file on disk [request setData:imageData withFileName:filename andContentType:@"image/jpeg" forKey:@"userfile"]; [request setDelegate:self]; [request startAsynchronous]; } ... - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:YES]; [picker.view removeFromSuperview]; // Access the uncropped image from info dictionary UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSString *imageName = @"uploaded.jpg"; // Save image //UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); [self uploadImage:UIImageJPEGRepresentation(imageView.image, 1.0) filename:imageName]; captionControls.hidden = NO; [textView becomeFirstResponder]; [picker release]; }
Here is the script I have receiving it.
$dir_dest = 'uploads/'; ob_start(); print_r($_FILES); $test = ob_get_contents(); ob_end_clean(); $db->insert('connecttest', array('ts'=>date('Y-m-d H:i:s'), 'tempfield'=>$test))->execute();
Here is the result I get
Array ( [(null)] => Array ( [name] => (null) [type] => (null) [tmp_name] => C:\Windows\Temp\phpCFF0.tmp [error] => 0 [size] => 0 ) )
Please help :(
解决方案I faced similar problem, i re-sized the image using
UIImage *im = [info objectForKey:@"UIImagePickerControllerOriginalImage"] ; UIGraphicsBeginImageContext(CGSizeMake(320,480)); [im drawInRect:CGRectMake(0, 0,320,480)]; newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData* imageData=UIImageJPEGRepresentation(newImage, 0.5);
and it started working, the height and width of the captured image will be more than 2000x2000 so reduce it and give a try.
[request setData:imageData withFileName:@"photo.jpg" andContentType:@"image/jpeg" forKey:@"file"];
and this is the php script.
$filename="uploaded"; $target_path = "uploads/"; $target_path = $target_path .$filename.".jpg"; if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo "uploaded an image"; } else{ echo "There was an error uploading the file, please try again!"; }
这篇关于新手有使用ASIHTTPRequest上传文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!