将文件上传到远程表单时,通用/未知的HTTP错误 [英] Generic/unknown HTTP error when uploading files to a remote form
问题描述
我有一个HTML 页面,其中包含用于将文件上传到SmartFile的表格.下面是我用来将Unity中游戏文件上传到此url的C#代码.
I have an HTML page with a form for uploading files to SmartFile. Below is the piece of C# code that I am using to upload the files from my game in Unity to this url.
WWWForm fileForm = new WWWForm();
string[] files = Directory.GetFiles(".", "*.txt");
fileForm.AddField("file", files[0]);
WWW www = new WWW("https://file.ac/xySSFOicMMk", fileForm);
不幸的是,它导致以下异常.怎么了?
Unfortunately it is leading to the below exception. What is wrong here?
发送分析数据时出现连接错误...错误:415不支持 媒体类型UnityEngine.Debug:LogError(Object) c__Iterator0:MoveNext()(位于Assets/Survey/Survey.cs:99) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator,IntPtr)
Connection error while sending analytics... Error:415 Unsupported Media Type UnityEngine.Debug:LogError(Object) c__Iterator0:MoveNext() (at Assets/Survey/Survey.cs:99) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
更新1:-我将第三行更改为fileForm.AddBinaryData("file", File.ReadAllBytes(files[i]), files[i], "text/plain");
Update 1:- I changed the 3rd line to fileForm.AddBinaryData("file", File.ReadAllBytes(files[i]), files[i], "text/plain");
我再也没有收到错误提示,但是即使www.isDone
返回的是true,仍然看不到文件正在上传.
I'm not getting the error anymore, but still can't see the file getting uploaded, even though www.isDone
is returning true.
更新2:-还尝试了 UnityWebRequest API,它导致了到
Update 2:- Tried the UnityWebRequest API as well, it led to
一般/未知的HTTP错误(400个响应代码)
Generic/unknown HTTP error (400 response code)
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
byte[] bytes = File.ReadAllBytes(files[0]);
files[0] = files[0].Replace(@".\","");
formData.Add(new MultipartFormFileSection("file", bytes, files[0], "text/plain"));
StartCoroutine(UploadFile(formData));
IEnumerator UploadFile(List<IMultipartFormSection> formData)
{
UnityWebRequest www = UnityWebRequest.Post("https://file.ac/xySSFOicMMk", formData);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
UnityWebRequest.Post
似乎也不适用于Unity版本2017.3.1f1(64位)中的WWWForm' or
List`
UnityWebRequest.Post
does not seem to work either with WWWForm' or
List` in the Unity version 2017.3.1f1 (64-bit)
推荐答案
将HttpWebRequest
与找到的解决方案一起使用此来解决Unity中的证书错误,最后让您省心.整个解决方案在此处发布- https://pastebin.com/wNQ46s0H
Used the HttpWebRequest
with the solution found here alongwith this to fix the certificate error in Unity and finally had some peace of mind. Entire solution is posted here - https://pastebin.com/wNQ46s0H
为了使WWWForm正常工作,您需要在表单中同时包含二进制和非二进制数据-
Seems like in order for WWWForm to work, you need to have both the binary and non-binary data in your form - HttpWebRequest post paramaters for AWS file upload request, leading to 400 error
这篇关于将文件上传到远程表单时,通用/未知的HTTP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!