I have an HTML [page][1] 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?
> 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)
**Update 1:-** I changed the 3rd line to `fileForm.AddBinaryData("file", File.ReadAllBytes(files[i]), files[i], "text/plain");`
I'm not getting the error anymore, but still can't see the file getting uploaded, even though `www.isDone` is returning true.
**Update 2:-** Tried the [UnityWebRequest][2] API as well, it led to
> Generic/unknown HTTP error (400 response code)
List formData = new List();
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 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!");
}
}
[1]: https://mouse-backtracker.herokuapp.com/GESFileUpload.html
[2]: https://docs.unity3d.com/Manual/UnityWebRequest-SendingForm.html
↧