使用ASIHTTPRequest从iPhone将文件上传到服务器 [英] Upload file onto Server from the IPhone using ASIHTTPRequest

查看:78
本文介绍了使用ASIHTTPRequest从iPhone将文件上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用ASIHTTPRequest库从iPhone将文件( login.zip )上传到Mac OS X Snow Leopard中内置的Apache服务器上.我的代码是:

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];

php代码是:

<?php $target = "upload/"; 
$target = $target . basename( $_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."; } ?> 

我不太明白为什么文件没有上传.如果有人可以帮助我.我已经坚持了5天了.

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.

先谢谢了 尼克

推荐答案

尝试一下:

[request setFile:filePath forKey:@"file"]; 

或者如果您可以将zip文件这样放入NSData

or if you can put your zip file into NSData like this

NSData *zipData = [[NSData alloc] initWithContentsOfFile:zipName];

并使用

[request setData:zip forKey:@"file"];

在服务器上尝试

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

$_FILES['file']['name']中的

['file']必须与您的请求中的forKey:@"file"相同

in $_FILES['file']['name'] the ['file'] MUST be the same as forKey:@"file" in your request

这篇关于使用ASIHTTPRequest从iPhone将文件上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆