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

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

  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&lt; 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。 记录(“已上传”+ files.Length +“文件成功”);
}
</ code> </ pre>

但是,文件是在 大小为0字节的目的地。</ p>

这是我的Uploader.php代码</ p>

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

if if($ tmpFilePath!=“”)
{
$ newFilePath =“ 上传/".$_ FILES ['files'] ['name'] [$ i];
if(!move_uploaded_file($ tmpFilePath,$ newFilePath))
$ uploadError = true;
}
}
if($ uploadError)
echo“Upload Error”;
else
echo“Uploaded Successfully”;
?&gt;
</ code> </ pre>

我使用了这个HTML 样品供参考。 在浏览器中,HTML代码完美无缺。 Unity中存在问题。</ p>

 &lt; form enctype =“multipart / form-data”action =“Uploader.php”method =“POST”&gt; 
选择一个 文件上传:
&lt; input type =“file”name =“files []”multiple =“multiple”/&gt;&lt; br&gt;
&lt; input type =“submit”value =“上传文件”/ &gt;
&lt; / form&gt;
</ code> </ pre>
</ div>

展开原文

原文

我正在尝试使用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>

dongtu0363
dongtu0363 谢谢你,你的帮助节省了大量的时间。问题是我在请求文件后没有屈服。一个简单的yieldreturn文件[i].SendWebRequest();解决了这个问题。其余的代码工作正常。
大约一年之前 回复
doutan5844
doutan5844 我只想使用多个http请求。你确定之前的Get调用不是导致问题吗?在表单中,您使用下一个文件内容覆盖密钥文件[]。我不确定这是AddBinaryData的工作原理...
大约一年之前 回复

1个回答



for loop </ code>中,在C#代码中,在请求文件后,我们必须在文件是 牵强。 所以在请求文件后使用 yield return files [i] .SendWebRequest(); </ code>将解决问题。
这是修改后的代码:</ p>

   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&lt; 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); \ n else
Debug.Log(“已上传”+ files.Length +“文件成功”);
}
</ c ode> </ pre>

其余代码没问题。 PHP代码没有变化。 HTML代码仅供参考。</ p>
</ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问