- Home /
Upload files to a remote form using C# in Unity
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?
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 API as well, it led to
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!");
}
}
Answer by Bunny83 · Apr 15, 2018 at 12:35 AM
Well, are you sure the web-endpoint actually works? If i try to upload a file through the webform I get an internal server error. Are you sure you don't need any kind of authentication?
Apart from that your first code does not upload any file but simply pass the file name / path as post data and not the actual file content. You have to use "AddBinaryData" when using the WWW class. If you can do uploads through the HTML form you should inspect the post to see what got actually send to your server. If you use FireFox you can use CTRL+SHIFT+E to open up the network analyse tool. When you submit your form you should be able to see the request. It gives you the request headers, cookies and parameters. I suspect you need to login before you can upload any files. Do you have any documentation of that web API?
No you dont need to login, did you just submit a test.txt file? Despite 500 error, the file is getting uploaded to the server. I also stated in the update1 above (in original post) that addbinarydata is not working.
I've messed around a bit with your server ^^. First of all your URL is wrong. When using "https://file.ac/xySSFOic$$anonymous$$$$anonymous$$k"
the server returns a 302 redirect to "https://file.ac/xySSFOic$$anonymous$$$$anonymous$$k/"
(note the trailing slash!!!). The WWW class often can't really handle redirects very well.
even when using it with the trailing slash, it is giving the same error. I have received the text2.txt and questions.pdf on my server which you just sent
Answer by vverma9 · Apr 15, 2018 at 05:47 AM
Found a workaround, finally some peace of mind https://stackoverflow.com/questions/49835903/upload-files-to-a-remote-form-using-c-sharp-in-unity?noredirect=1#comment86691280_49835903
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting info using WWW & PHP 3 Answers
Log in on a website and get data 0 Answers
Download multiple files from server 2 Answers