javascript - Grabbing a file sent from Django on front-end -
i trying post file via http-request (something similar curl -f request). want best described following code:
def my_view(request): string_to_return = '<?xml version="1.0" encoding="utf-8"?>...' file_to_send = contentfile(string_to_return) response = httpresponse(file_to_send,'application/xml') response['content-length'] = file_to_send.size response['content-disposition'] = 'attachment; filename="somefile.xml"' return response $.get('/my_view/', function(response){ var formdata = new formdata(); // file = ??? how grab file ??? formdata.append("thefile", file); xhr.send(formdata); });
basically, question here how grab xml file in client. in advance!
some notes
- i need file content generated on server-side
- i need file passed client-side , sent external server via http request.
okay trying download file django , upload server javascript app. haven't done before according https://developer.mozilla.org/en-us/docs/web/api/xmlhttprequest/sending_and_receiving_binary_data shouldn't difficult.
first, download 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 step sendblob(blob, 'http://www.example.com/other_url'); }; oreq.send();
next upload binary file other server:
function sendblob(blob, url){ var oreq = new xmlhttprequest(); oreq.open("post", url, true); oreq.onload = function (oevent) { // uploaded. }; oreq.send(blob); }
Comments
Post a Comment