使用来自iPhone应用程序的ASIHTTPRequest在服务器中上传图像文件

时间:2021-12-23 16:32:56

I want to upload images in server using ASIHTTPRequest library in my IPhone app.

我想在我的iPhone应用程序中使用ASIHTTPRequest库在服务器上传图像。

For image upload, i am using ASIFormDataRequest to upload into server.

对于图像上传,我使用ASIFormDataRequest上传到服务器。

I have tried the below codes in my app but it couldn't working and image didn't uploaded in server.

我在我的应用程序中尝试了以下代码,但它无法正常工作,图像没有上传到服务器中。

NSURL *url = [NSURL URLWithString:@"http://www.anglerdemo.com/Appln/Aghaven_iphone/Uploadphoto.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.requestMethod = @"POST";
NSString *fileName = @"iphone.jpg";
[request addPostValue:fileName forKey:@"name"];

// Upload an image
UIImage *img = [UIImage imageNamed:fileName];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSLog(@"imageData ==> %@", imageData);
[request setData:imageData withFileName:fileName andContentType:@"image/jpeg" forKey:@"image"];
[request setDelegate:self]; 
[request startAsynchronous];

I have tried above codes in my app, request successfully executed and i got the alert message in "requestFinished" delegate method of ASIHTTPRequest. but image didn't uploaded in server.

我已在我的应用程序中尝试了上述代码,请求已成功执行,并且我在ASIHTTPRequest的“requestFinished”委托方法中收到了警报消息。但是图像没有上传到服务器中。

- (void)requestFinished:(ASIHTTPRequest *)request
 {

[[[[UIAlertView alloc] 
   initWithTitle:@"Message" 
   message:@"Success!!!" 
   delegate:self 
   cancelButtonTitle:@"OK" 
   otherButtonTitles:nil] 
  autorelease] 
 show];

NSLog(@"success ==> ");
}

I have tried php file script for upload is below.

我试过上传的php文件脚本如下。

<?php
   $uploaddir = 'upload_files/';
   $file = basename($_FILES['image']['name']);
   $uploadfile = $uploaddir . $file;

   if(move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))
  {
   echo "Photo has been saved successfully!…;
  }
  else {
echo "Failed";
  }
?>

Please help in this regards.

请帮助解决这个问题。

Thanks!!!

1 个解决方案

#1


1  

I think the first thing I would do is add

我想我要做的第一件事就是添加

NSLog(@"%@", [request responseString]);

to requestFinished: just to be sure you see "Failed" being logged. This will prove the problem is with the attempt to call move_upload_file, which I would guess is where things go wrong.

requestFinished:只是为了确保你看到“失败”被记录。这将证明问题是尝试调用move_upload_file,我猜这是出错的地方。

If that's the case, I would be pretty sure that the user your webserver runs as doesn't have write permissions to the upload_files directory.

如果是这种情况,我很确定您的网络服务器运行的用户没有对upload_files目录的写权限。

#1


1  

I think the first thing I would do is add

我想我要做的第一件事就是添加

NSLog(@"%@", [request responseString]);

to requestFinished: just to be sure you see "Failed" being logged. This will prove the problem is with the attempt to call move_upload_file, which I would guess is where things go wrong.

requestFinished:只是为了确保你看到“失败”被记录。这将证明问题是尝试调用move_upload_file,我猜这是出错的地方。

If that's the case, I would be pretty sure that the user your webserver runs as doesn't have write permissions to the upload_files directory.

如果是这种情况,我很确定您的网络服务器运行的用户没有对upload_files目录的写权限。