- Home /
Unable to upload Image to server with MultipartFormFileSection , WWWForm and Upload Handler
I have tried every possible solution for uploading image to server using unity 5.6.2 , but still there is no success. I used legacy and latest methods as well .The server responds with response code 204. But when I use postman to check , it works fine and file is uploaded. Content-Type doesn't matter either use it or not. Here is my script. Thanx
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
using System;
public class SendData : MonoBehaviour {
public void ButtonPressed()
{
StartCoroutine (sendImage ());
}
IEnumerator sendImage()
{
byte[] img = File.ReadAllBytes(Application.dataPath + "/scree.png");
Debug.Log ("File Length " + img.Length);
List<IMultipartFormSection> requestData = new List<IMultipartFormSection>();
requestData.Add( new MultipartFormFileSection("file" , img , "scree.png" , "image/png"));
UnityWebRequest request = UnityWebRequest.Post("http://mysite.com/uploadfile" , requestData);
request.SetRequestHeader("Content-Type", "multipart/form-data");
request.SetRequestHeader("EventID","1");
request.SetRequestHeader("FileName","scree.png");
request.chunkedTransfer = false;
yield return request.Send();
print("request completed with code: " + request.responseCode);
if(request.error != null)
{
print("Error: " + request.error);
}
else
{
print("Request Response: " + request.downloadHandler.text);
}
}
}
Comment