Hi, I'm trying to send a file to a server using WWWForm, but I have a problem of $_FILES receiving it, it doesn't receive any data, I checked that both (the $_FILES["name"] and filed name) are the same.
I've been reading that maybe is because the form is sent with application/x-www-form-urlenconded, but I tried to put in the headers the Content-Type as "multipart/form-data" and it results in a error "400 Bad Request".
Also I tried to use UnityWebRequest and FormMultipartFileSection instead of WWW and WWWForm, but I'm getting a error code 400 with "Generic/Unknown HTTP error"
Any clues of what is happening? (I'll post my cs and php codes below)
Regards
IEnumerator UploadFileCo(string localFileName, string uploadURL)
{
WWW localFile = new WWW("file:///data/user/0/com.HeXentic.IldemaroApp/cache/PickedImage.jpg");
Debug.Log("IMAGE PATH TRYING TO LOAD IS: " + localFile.url);
yield return new WaitUntil(() => localFile.isDone);
if (localFile.error == null)
Debug.Log("Loaded file successfully");
else
{
Debug.Log("Open file error: " + localFile.error);
yield break; // stop the coroutine here
}
Texture2D tex = new Texture2D(163, 224, TextureFormat.RGBA32, false);
WWW w = new WWW("file:///data/user/0/com.HeXentic.IldemaroApp/cache/PickedImage.jpg");
yield return new WaitUntil(() => w.isDone);
//tex = texture;
if (w.bytesDownloaded > 0 && w.error == null)
{
w.LoadImageIntoTexture(tex);
for (int i = 0; i <= profilesImages.Length - 1; i++)
{
profilesImages[i].GetComponent().texture = tex;
}
//byte[] imageBytes = tex.EncodeToJPG();
//FileStream fileSave;
//fileSave = new FileStream(Application.persistentDataPath + "/Save/picture.jpg", FileMode.Create);
//BinaryWriter binary;
//binary = new BinaryWriter(fileSave);
//binary.Write(imageBytes);
//fileSave.Close();
/* for (int i = 0; i <= imageBytes.Length - 1; i++)
{
Debug.LogWarning("IMAGE BYTES INDEX: " + i + " VALUE = " + imageBytes.GetValue(i));
}*/
//WWWForm postForm = new WWWForm();
// version 1
//postForm.AddBinaryData("theFile",localFile.bytes);
// version 2
//postForm.AddBinaryData("archivo-a-subir", w.bytes , "demo.jpg" + ".jpg", "image/jpeg");
//Dictionary headers = postForm.headers;
//headers.Add("enctype", "multipart/form-data");
//postForm.AddField("test","test");
Debug.Log("Trying to upload the file: " + tex.GetRawTextureData());
Debug.Log("In the URL: " + uploadURL);
Debug.Log("With the name of: " + UserAccountInfo.instance.email.Replace(".", "_") + ".png");
//WWW upload = new WWW(uploadURL, postForm.data, headers);
//////////////////////////////////////////////
/// CON UNITY WEB REQUEST
//////////////////////////////////////////////
/*
List formData = new List();
formData.Add(new MultipartFormFileSection("archivo-a-subir",tex.EncodeToPNG(), UserAccountInfo.instance.email.Replace(".","_") + ".png","image/png"));
//WWWForm postForm = new WWWForm();
//postForm.AddBinaryData("archivo-a-subir", tex.EncodeToPNG(), UserAccountInfo.instance.email.Replace(".", "_") + ".png", "image/png");
//Dictionary headers = postForm.headers;
//headers["Content-Type"] = "multipart/form-data";
//byte[] formData = postForm.data;
UnityWebRequest upload = UnityWebRequest.Post(uploadURL, formData);
upload.SetRequestHeader("Content-Type", "multipart/form-data");
upload.SetRequestHeader("Content-Disposition","form-data");
yield return upload.SendWebRequest();
*/
//////////////////////////////////////////////
/// CON WWW
//////////////////////////////////////////////
WWWForm postForm = new WWWForm();
Dictionary headers = new Dictionary();
headers.Add("Content-Type", "multipart/form-data");
foreach (KeyValuePair kvp in headers)
{
//textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
Debug.Log("Key = " + kvp.Key + ", Value = "+ kvp.Value + " ");
}
//headers["Content-Disposition"] = "form-data";
//postForm.AddBinaryData("archivo-a-subir", w.bytes , UserAccountInfo.instance.email.Replace(".", "_") + ".jpg", "image/jpeg");
postForm.AddBinaryData("archivo-a-subir", tex.EncodeToPNG(), UserAccountInfo.instance.email.Replace(".", "_") + ".png", "file");
//postForm.AddField("test","test");
WWW upload = new WWW(uploadURL, postForm.data, headers ); // test 1
//WWW upload = new WWW(uploadURL, postForm.data); // Test 2
yield return upload;
yield return new WaitUntil(() => upload.isDone);
if (upload.error == null)
{
Debug.Log("upload done");
//Debug.Log("ECHO OF RESPONSE: " + upload.downloadHandler.text);
Debug.Log("ECHO OF RESPONSE: " + upload.text);
}
else
{
//Debug.Log("Error during upload: " + upload.responseCode + ": " + upload.error);
Debug.Log("Error during upload: " + upload.error);
}
}
else
{
Debug.LogError("ERROR EN OBTENER LA TEXTURA DE LA UBICACION EN EL TELEFONO");
}
}
subir-archivos.php (in the server)
↧