- Home /
Question by
ajavjari · Aug 26, 2019 at 12:22 PM ·
apiwebrequestwwwformweb services
Image upload API works with postman but not working with Unity.
I'm using Unity 2018.3.14f1.
I have created API for uploading image in php. Which is working well in the postman and not working in unity editor, All the time it gives me following log "HTTP/1.1 403 Forbidden".
Here is my code:
IEnumerator UploadImage()
{
yield return new WaitForEndOfFrame();
Texture2D screenImage = new Texture2D(Screen.width, Screen.height);
screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenImage.Apply();
byte[] imageBytes = screenImage.EncodeToPNG();
WWWForm form = new WWWForm();
form.AddBinaryData("file", imageBytes, "screenShot.png", "image/png");
using (var w = UnityWebRequest.Post(screenShotURL, form))
{
yield return w.SendWebRequest();
if (w.isNetworkError || w.isHttpError)
{
print(w.error);
}
else
{
print("Finished Uploading Screenshot");
}
}
}
Here is php script :
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 20000000000))
{
if ($_FILES["file"]["error"] > 0) {
//echo "Return Code: " . $_FILES["file"]["error"] . "";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "";
echo "Type: " . $_FILES["file"]["type"] . "";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "";
if (file_exists("Images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "Images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "Images/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
Comment
Your answer
Follow this Question
Related Questions
How to use UnityWebRequest to upload a File via Invision REST API 0 Answers
Calling API via proxy 0 Answers
Pass Header Data in UnityWebRequest 2 Answers
Legitimate Instagram API or at least WEB requests? 0 Answers