doutui2883 2019-05-15 18:28 采纳率: 100%
浏览 2748
已采纳

如何使用UnityWebRequest.Post()将多个文件上传到服务器;

我正在尝试使用UnityWebRequest.Post()上传多个文件,这是我的代码。

 public void UploadFiles()
 {
     string[] path = new string[3];
     path[0] = "D:/File1.txt";
     path[1] = "D:/File2.txt";
     path[2] = "D:/File3.txt";

     UnityWebRequest[] files = new UnityWebRequest[3];
     WWWForm form = new WWWForm();

     for (int i = 0; i < files.Length; i++)
     {
         files[i] = UnityWebRequest.Get(path[i]);
         form.AddBinaryData("files[]", files[i].downloadHandler.data, Path.GetFileName(path[i]));
     }

     UnityWebRequest req = UnityWebRequest.Post("http://localhost/File%20Upload/Uploader.php", form);
     yield return req.SendWebRequest();

     if (req.isHttpError || req.isNetworkError)
         Debug.Log(req.error);
     else
         Debug.Log("Uploaded " + files.Length + " files Successfully");
 }

但是,文件是在目标位置创建的,大小为0字节。 这是我的Uploader.php代码

 <$php
   $total = count($_FILES['files']['name']);
   $uploadError = false;
   for ( $i = 0; $i < $total; $i++)
   {
     $tmpFilePath = $_FILES['files']['tmp_name'][$i];

     if ($tmpFilePath != "")
     {
         $newFilePath = "Uploads/".$_FILES['files']['name'][$i];
         if (!move_uploaded_file($tmpFilePath, $newFilePath))
             $uploadError = true;
     }
   }
   if ($uploadError)
       echo "Upload Error";
   else
       echo "Uploaded Successfully";
 ?>

我将此HTML示例用作参考。 在浏览器中,HTML代码可以完美运行。 而在Unity中却有问题。

 <form enctype="multipart/form-data" action="Uploader.php" method="POST">
     Choose a file to Upload:
     <input type="file" name="files[]" multiple="multiple" /><br>
     <input type="submit" value="Upload File" />
 </form>
  • 写回答

1条回答 默认 最新

  • douxian7808 2019-05-15 19:08
    关注

    In for loop, in the C# code, after requesting the file, we must yield while the file is fetched. so using yield return files[i].SendWebRequest(); after requesting the file will solve the problem. Here is the modified code:

    IEnumerator UploadMultipleFiles()
    {
        string[] path = new string[3];
        path[0] = "D:/File1.txt";
        path[1] = "D:/File2.txt";
        path[2] = "D:/File3.txt";
    
        UnityWebRequest[] files = new UnityWebRequest[path.Length];
        WWWForm form = new WWWForm();
    
        for (int i = 0; i < files.Length; i++)
        {
            files[i] = UnityWebRequest.Get(path[i]);
            yield return files[i].SendWebRequest();
            form.AddBinaryData("files[]", files[i].downloadHandler.data, Path.GetFileName(path[i]));
        }
    
        UnityWebRequest req = UnityWebRequest.Post("http://localhost/File%20Upload/Uploader.php", form);
        yield return req.SendWebRequest();
    
        if (req.isHttpError || req.isNetworkError)
            Debug.Log(req.error);
        else
            Debug.Log("Uploaded " + files.Length + " files Successfully");
    }
    

    Rest of the code is fine. No changes in PHP code. HTML code is only for reference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛