Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

php - Newbie having issues with uploading a file using ASIHTTPRequest

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:WindowsTempphpCFF0.tmp
            [error] => 0
            [size] => 0
        )

)

Please help :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...