- Home /
Failed to upload image over custom server
Using web service, I was trying to upload image to web server. At present I want to upload profile picture to custom server but in this always, I was getting this error in Unity editor.
For this, I was using following code:
void Start ()
{
StartCoroutine (UploadPNG ());
}
IEnumerator UploadPNG ()
{
// We should only read the screen after all rendering is complete
yield return new WaitForEndOfFrame ();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels (new Rect (0, 0, width, height), 0, 0);
tex.Apply ();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG ();
Destroy (tex);
// Create a Web Form
WWWForm form = new WWWForm ();
form.AddBinaryData ("profile_pic", bytes, "screenShot.png", "image/png");
// Upload to a cgi script
WWW w = new WWW (screenShotURL, form);
yield return w;
if (!string.IsNullOrEmpty (w.error)) {
print (w.error);
} else {
print ("Finished Uploading Screenshot");
}
}
This is similar code that I found from here: WWWForm
As well screenshot after running url in PostMan.
Here is actual url to test:
private string screenShotURL = "http://photo-competition.igts.co.in/restapi/image_upload_demo_ws";
Please suggest me at which place I have done mistake.
EDIT: Following two link give you source code used from PHP side.
Answer by siddharth3322 · Jun 29, 2016 at 09:55 AM
Now I have answer for this question so that I am writing here. By considering that it may be useful to others.
Basically this problem is from server side. If I become specific towards problem then its IP address blockage problem. I transferred my web server source code from one server to other. Then after same above code worked perfectly and image gets uploaded without any problem.
So please before wasting any time, check your server side first because this kind of error definitely belong to server as per my understanding.
Answer by gorsefan · Jun 22, 2016 at 05:26 PM
It looks to me like a sub-optimally configured web server. If I hit the screenShotURL I get a 404 response, but I also get served XML;
<?xml version="1.0" encoding="utf-8"?>
<xml><status>0</status><error>Unknown method.</error></xml>
Hit another random page on that domain, you get a proper 404 response & page.
Now this is probably because I am trying to GET a URL which expects a POST along with other data, such as probably a hidden form field with a method name the web server expects. Responding with a 404 in this situation seems like poor HTTP to me as it looks like there is a resource there, it just doesn't like your request, or the web server is configured to serve different 404 responses from /restapi than the rest of the domain, which is weird. IMO it should respond with a 400 Bad Request, or 403 Forbidden, depending.
Short version, it looks to me like you are missing some data to fulfil the request & this isn't a Unity question. I'd look for a method parameter, check cookies, do you need to be signed in? etc etc. And read their restapi documentation. Either way, only the owner of that service can really help you I think...
Yes, you are right, by default i was using post method submission.
I think parameters and all other settings are correct, you can check this by postman screenshot that I have pasted.
Rest API documentation means?
"I think parameters and all other settings are correct, you can check this by postman screenshot that I have pasted."
That is not possible because I dont know what the parameters are.
When you post to a web service, as you are doing, the types of data you post have to match the types of data expected by the PHP (or whatever) application running on the server. This matching of data types is what an API is. And a description of those data types is what I mean by "Rest API documentation" (REST can be understood as a type of web application architecture). No-one can know this apart from the person who wrote the PHP. You might be able to guess it from an HT$$anonymous$$L page that does what you are trying to do in Unity, but you need documentation of what the server code expects because it looks like you are not sending the correct data.
@gorsefan, now i have added PHP source code link so that you can get more clear idea. Thanks for your suggestions.
Answer by Abdis · Oct 09, 2018 at 04:53 PM
the php source code image seems doesn't longer exists, so if you don't mind, could you reupload the image or share your php code, i really need it @siddharth3322
this is a really old question so at present even I don't have those images as well ... I have deleted those
so, could i ask something, Unity send it as binary data, right? if Php side receive it, does it need to be converted again into file? or do i could handle it directly by using $_FILES["fileToUpload"]["tmp_name"] ? i have handle it directly like these, but it seems doesn't work