2

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

 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天。

提前感谢尼克

4

2 回答 2

4

尝试这个:

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

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

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

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

于 2011-03-21T10:14:04.657 回答
0

您使用的字段名称是否不匹配?

看起来您在 iphone 上使用“文件”:

[request setPostValue:@"login.zip" forKey:@"file"];  

但在服务器上“上传”:

$target = $target . basename( $_FILES['uploaded']['name']) ; 

尝试将这些更改为相同。

于 2010-07-21T19:03:53.510 回答