[Fixed]-Grabbing a file sent from Django on front-end

1👍

Okay so your trying to download a file from django and upload it to another server from your javascript app. I haven’t done this before but according to https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data it shouldn’t be too difficult.

First, download the binary file:

var oReq = new XMLHttpRequest();
oReq.open("GET", "/my_view/", true);
oReq.responseType = "blob";

oReq.onload = function(oEvent) {
  var blob = oReq.response;
  // ...see below for this step
  sendBlob(blob, 'http://www.example.com/other_url');
};

oReq.send();

Next upload the binary file to your other server:

function sendBlob(blob, url){
    var oReq = new XMLHttpRequest();
    oReq.open("POST", url, true);
    oReq.onload = function (oEvent) {
      // Uploaded.
    };

    oReq.send(blob);
}

Leave a comment