【问题标题】:Upload image file in server using ASIHTTPRequest from IPhone app使用来自 iPhone 应用程序的 ASIHTTPRequest 在服务器中上传图像文件
【发布时间】:2011-12-28 09:29:38
【问题描述】:

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

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

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

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

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

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

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

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

我已经尝试过上传的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";
  }
?>

请在这方面提供帮助。

谢谢!!!

【问题讨论】:

    标签: iphone asihttprequest image-upload asiformdatarequest


    【解决方案1】:

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

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

    到 requestFinished:只是为了确保您看到“失败”被记录。这将证明问题出在调用 move_upload_file 的尝试上,我猜这就是问题所在。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 2013-01-13
      相关资源
      最近更新 更多