Upload file onto Server from the IPhone using ASIHTTPRequest
I've been trying to upload a file (login.zip) using the ASIHTTPRequest libraries from the IPhone onto the inbuilt Apache Server in Mac OS X Snow Leopard. My code is:
NSString *urlAddress = [[[NSString alloc] initWithString:self.uploadField.text]autorelease];
NSURL *url = [NSURL URLWithString:urlAddress];
ASIFormDataRequest *request;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"login.zip"];
NSData *data = [[[NSData alloc] initWithContentsOfFile:dataPath] autorelease];
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@"login.zip" forKey:@"file"];
[request setData:data forKey:@"file"];
[request setUploadProgressDelegate:uploadProgress];
[request setShowAccurateProgress:YES];
[request setDelegate:self];
[request startAsynchronous];
The php code is :
<?php $target = "upload/";
$target = $target . bas开发者_Python百科ename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{ echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; }
else
{ echo "Sorry, there was a problem uploading your file."; } ?>
I don't quite understand why the file is not uploading. If anyone could help me. I've stuck on this for 5 days straight.
Thanks in advance Nik
Try this:
[request setFile:filePath forKey:@"file"];
or if you can put your zip file into NSData like this
NSData *zipData = [[NSData alloc] initWithContentsOfFile:zipName];
and send it to server with
[request setData:zip forKey:@"file"];
on the server try this
<?php
$dir = "/var/www/your_directory/";
$path = $dir . $_FILES['file']['name'];
//move_uploaded_file($_FILES['file']['tmp_name'], $path);
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
//return ok! :)
}
else {
// return -> echo"There's been a problem uploading your file. Please try again";
}
?>
in $_FILES['file']['name']
the ['file']
MUST be the same as forKey:@"file"
in your request
Is there a mismatch in the field names you're using?
It looks like you're using "file" on the iphone:
[request setPostValue:@"login.zip" forKey:@"file"];
but 'uploaded' on the server:
$target = $target . basename( $_FILES['uploaded']['name']) ;
Try changing these to be the same.
精彩评论