Javascript file upload not working for binary file while it's working on image URL! Help!

93 views
Skip to first unread message

ching...@gmail.com

unread,
Mar 2, 2015, 6:10:51 PM3/2/15
to im...@googlegroups.com
<html>
<head>
<script>
var client = new XMLHttpRequest();

function upload()
{
var file = document.getElementById("file");
// working var formData = { image: "https://www.google.com/images/srpr/logo11w.png"};

var formData = new FormData();
formData.append("image", file.files[0]); // This is not working??

client.open("POST", "https://api.imgur.com/3/upload", true);
client.setRequestHeader("Authorization", "Client-ID xxx");
client.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
client.send(JSON.stringify(formData));
}

client.onreadystatechange = function()
{
if (client.readyState == 4 && client.status == 200)
{
document.write(client.responseText);
}
}
</script>
</head>

<body>
<input type="file" id="file" name="file" />
<input type="button" value="upload" onclick="upload()" />
</body>
</html>

@jasdev

unread,
Mar 3, 2015, 12:58:51 PM3/3/15
to im...@googlegroups.com
Make sure you have an additional form param for the `type` = `file`


Best,

Jasdev
Message has been deleted

ching...@gmail.com

unread,
Mar 4, 2015, 1:54:23 PM3/4/15
to im...@googlegroups.com
Hi Jasdev,

Thanks for reply. I updated the code a bit, but it is still not working.

Please advise.

Thank you.


<html>
<head>
<script>
var client = new XMLHttpRequest();

function upload()
{
var file = document.getElementById("file");

var formData = new FormData();
formData.append("image", file.files[0]);
formData.append("type", "file");

client.open("POST", "https://api.imgur.com/3/upload", true);
client.setRequestHeader("Authorization", "Client-ID xxx");
client.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
client.send(JSON.stringify(formData));
}

/* Check the response status */
client.onreadystatechange = function()
{
if (client.readyState == 4 && client.status == 200)
{
document.write(client.responseText);
}
}
</script>
</head>

<body>
<form>
<input type="file" id="file" name="file" />
<input type="button" value="upload" onclick="upload()" />
</form>

</body>
</html>

@jasdev

unread,
Mar 11, 2015, 1:57:40 PM3/11/15
to im...@googlegroups.com
Sorry for the late reply! Try using this example as reference, might help you out!

Reply all
Reply to author
Forward
0 new messages