jquery - How to set Request Headers before it being loaded in an iframe -
i need download file 'content-disposition' header being set "attachment" server. using jquery.ajax get , on success setting hidden iframe src url, gives me pop-up file download. , working fine in browsers. want change custom request headers encrypt file before & download. used jquery.ajax pre-request callback function beforesend it.
i able encrypted file can observe in firebug iframe still shows non encrypted file download. after inspecting can iframe requesting new .
code
$.ajax({ url: "/tutorial.text", beforesend: function(xhr) { xhr.setrequestheader("password_header", userpwd); }, success: function() { $("#hidden_iframe").attr("src", this.url); } }); and working on internet explorer. how can force iframe use available resource rather requesting new get. or how can setrequestheader in iframe or need jquery.ajax task there best way download content-disposition header being set attachment files directly server.
this solution doesn't uses iframe or form. uses xhr custom header on resource supports cors (just random svg internet example). key differences in approach xhr.responsetype = 'arraybuffer'; , link blob href , download attributes:
// initial data var url = '//enable-cors.org/img/cloud-download.svg'; var password = '123456'; // download url arraybuffer function download (url, password, cb) { var xhr = new xmlhttprequest(); xhr.open('get', url, true); xhr.responsetype = 'arraybuffer'; // xhr.setrequestheader('password_header', password); xhr.onload = function () { cb(xhr.response); }; xhr.send(null); } // receive binary content of url // create blob link , click on download(url, password, function (arraybuffer) { var file = new file([arraybuffer], 'some filename'); var = document.createelement('a'); a.setattribute('href', window.url.createobjecturl(file)); a.setattribute('download', 'file-name-of-download.ext'); // in firefox `a.click()` works if `a` element in dom, so... document.documentelement.appendchild(a); a.click(); console.log('done'); }); tested in chrome57 , ff54.
Comments
Post a Comment