Vitam
January 25, 2017, 5:45pm
1
I need a way to upload a screenshot to Imgur (or other img server) and get the url to it.
I was able to replicate in the editor what this user did: How can I upload a screenshot directly to Imgur? - Questions & Answers - Unity Discussions
But when I run it in my WebGL application, it crashes the browser while trying to upload the image.
Can anyone shed a light on what is the problem here?
An alternative way to do the same thing would help as well!
Edit:
I just realized that the code I tried didn’t work because System.Net is not supported in WebGL.
Is there a way to accomplish what I am trying to do, without it?
Vitam
January 25, 2017, 11:51pm
2
I was able to make it work with UnityWebRequest and WWWForm. Here is the code:
IEnumerator UploadToImgur(byte[] rawImage) {
string uploadedUrl = "";
UnityWebRequest www;
WWWForm wwwdata = new WWWForm();
wwwdata.AddField("image", Convert.ToBase64String(rawImage));
wwwdata.AddField("type", "base64");
www = UnityWebRequest.Post("https://api.imgur.com/3/image.xml", wwwdata);
string clientID = "YOUR IMGR CLIENT-ID HERE";
www.SetRequestHeader("AUTHORIZATION", "Client-ID " + clientID);
// if you want to display a loading image, here is where you should call it
yield return www.Send();
// disable loading image
if (www.isError) {
Debug.Log(www.error);
}
else {
Debug.Log("Upload complete!");
Debug.Log("Data: " + www.downloadHandler.text);
XDocument xDoc = XDocument.Parse(www.downloadHandler.text);
uploadedUrl = xDoc.Element("data").Element("link").Value;
}
Debug.Log(uploadedUrl); //This holds the url to the img
}
Great code …I found this also help and it is work too
I have been trying to follow Imgur's api for uploading images So far I have been able to get it to properly recognize my Client ID. I know that I am getting through, but I still get a 400 error, which according to Imgur's documentation means ...