ios4 - 在iPhone中使用ASIHTTPRequest将图像上传到服务器

标签 ios4 asihttprequest

我必须将图像从 iphone 上传到服务器,为此我使用 ASIHTTPRequest。我设置了一个循环来上传七个文件。但是在只上传最后一个文件之后,有人可以指出我哪里出错了。

我使用以下代码进行上传:

for (int i=1; i<8; i++) 
    {
        NSString* filename = [NSString stringWithFormat:@"Photo%d.jpg", i];
        NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:filename];
        [request setFile:path forKey:[NSString stringWithFormat:@"file"]];
    }

    [request startAsynchronous];
    [resultView setText:@"Uploading data..."];

My Php file code is as following : 



 <?php
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

        if (file_exists("vinay/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "vinay/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "http://serverpath" . $_FILES["file"]["name"];
          }
        }
    ?> 

最佳答案

您正在覆盖名为 file 的 key ,并且您需要使用队列。

[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];

for (int i=1; i<8; i++) 
{
    NSString* filename = [NSString stringWithFormat:@"Photo%d.jpg", i];
    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:filename];
    [request setFile:path forKey:[NSString stringWithFormat:@"file%d", i]];
    [[self networkQueue] addOperation:request];
}
[[self networkQueue] go];

关于ios4 - 在iPhone中使用ASIHTTPRequest将图像上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4680503/

相关文章:

ios - ASIHTTPREQUEST 无内容长度响应(大型 PDF 文件)

iphone - 如何更改 UIAlertView 中的按钮颜色

iphone - 使用 XCode 3.2.3 编译 iphone 3.1.3?

ios - 在特定时间在 Iphone 中显示警报/通知(由用户或开发人员设置)

iphone - 在 iPhone 上绘制网格或使用图像

iphone - 常规低通滤波器和自适应低通滤波器有什么区别?

objective-c - ASIHTTPRequest 在请求 :willRedirectToUrl: is implemented 时停止下载

iphone - 为什么当另一个线程正在运行时 NSTimer 会被阻塞?

ios - 从 iPhone 应用程序在 Facebook 'feed' 上发布图片

objective-c - ASIHTTPRequest 在中断 Internet 连接后导致崩溃