Search code examples
javascriptfile-uploadxmlhttprequestform-dataimgur

xhr html file upload on imgurl gives bad request


I want to upload an image to imgur using their API; using javascript (node.js). I always get this as response:

{"data":{"error":"Image format not supported, or image is corrupt.","request":"/3/upload","method":"POST"},"success":false,"status":400}

Here's the code I wrote:

ImgUrlHelper = 
{
InitUploadValidateFile :function(element)
{
     var file = element.value;
     if(file === "")
        return;

     if(file != '')
     { 
         var valid_extensions = /(.jpg|.jpeg|.gif|.png)$/i;   
          if(valid_extensions.test(file))
          {
                console.log("ok");
                this.Upload(file);
          }
          else
                console.log("notok");
     }
     else
        console.log("notok");
},           

Upload: function(file) {

    var fd = new FormData(); 
    fd.append("image", file); 
    var xhr = new XMLHttpRequest(); 

    xhr.open("POST", "https://api.imgur.com/3/upload", true); 
    xhr.onload = this.OnLoad;
    xhr.onerror = this.OnNetworkError;
    xhr.onreadystatechange = this.OnReadyStateChange;
    xhr.onprogress= this.OnProgress;

    xhr.setRequestHeader('Authorization', 'Client-ID xxxxxxxxxxxx');
    xhr.send(fd);
},

OnReadyStateChange:function(xhr){
    if(xhr.readyState == 4 && xhr.status == 200)
    {
        var link = JSON.parse(xhr.responseText).data.link;
        console.log(link);    
    }
    if(xhr.status != 200)
    {
        console.log(xhr.status);


        //log error
    }
},

OnNetworkError:function(xhr)
{
    //log error
    console.log("network error while uploading.");
    console.log(xhr);
},

OnProgress : function(e)
{
    if (e.lengthComputable) {
      var percentComplete = (e.loaded / e.total) * 100;
      console.log(percentComplete + '% uploaded');
    }
},

OnLoad :  function(res) {

}
}

I call this by using an onchange event on an input type="file":

onchange="ImgUrlHelper.InitUploadValidateFile(this)"

I assume there is a problem reading the file that i selected with the file upload control, here:

fd.append("image", file); 

At that point, file contains the string: "C:\fakepath\img.jpg". Do I maybe need to actually encode the image?


Solution

  • You should upload a File object, not a string.

    Change var file = element.value; to var file = element.files[0];:

    // assuming that element is an <input type=file>
    InitUploadValidateFile: function(element) {
        var file = element.files[0];
        if (!file)
            return;
    
        var valid_extensions = /(\.jpg|\.jpeg|\.gif|\.png)$/i;
        if (valid_extensions.test(file.name)) {
            console.log("ok");
            this.Upload(file);
        } else {
            console.log("notok");
        }
    },