admin管理员组文章数量:1320652
I have written ajax code to set request headers in url
and convert that to a blob and pass to a function showFile (blob);
The blob
is then processed and downloaded in pdf format
The blob
value obtained in the code is ing as undefined
. Can someone please help me to resolve the issue`
var resolved = function (url) {
var showFile = function (blob) {
var newBlob = new Blob([blob], {type:"application/pdf"})
const data = window.URL.createObjectURL(newBlob);
var link = document.createElement('a');
link.href = data;
link.download = options.name;
link.click();
setTimeout(function () {
window.URL.revokeObjectURL(data);
}, 100)
}
var jwtToken = localStorage.getItem("jwtToken");
var headerObj = {"Authorization": "Bearer " + jwtToken}
var xhr = new XMLHttpRequest();
$.ajax({
dataType:'blob',
type:'GET',
url:url
}).done(function(blob){
showFile(blob);
});
};
I have written ajax code to set request headers in url
and convert that to a blob and pass to a function showFile (blob);
The blob
is then processed and downloaded in pdf format
The blob
value obtained in the code is ing as undefined
. Can someone please help me to resolve the issue`
var resolved = function (url) {
var showFile = function (blob) {
var newBlob = new Blob([blob], {type:"application/pdf"})
const data = window.URL.createObjectURL(newBlob);
var link = document.createElement('a');
link.href = data;
link.download = options.name;
link.click();
setTimeout(function () {
window.URL.revokeObjectURL(data);
}, 100)
}
var jwtToken = localStorage.getItem("jwtToken");
var headerObj = {"Authorization": "Bearer " + jwtToken}
var xhr = new XMLHttpRequest();
$.ajax({
dataType:'blob',
type:'GET',
url:url
}).done(function(blob){
showFile(blob);
});
};
Share
Improve this question
edited May 5, 2018 at 16:53
Musa
97.7k17 gold badges122 silver badges143 bronze badges
asked May 5, 2018 at 11:09
anonymousanonymous
1011 gold badge4 silver badges11 bronze badges
2
- What version of jQuery are you using? – Musa Commented May 5, 2018 at 17:26
- JQuery 3.1.1 version – anonymous Commented May 5, 2018 at 18:03
1 Answer
Reset to default 5If you need a blob response from jQuery ajax, set the responseType
field of xhrFields
to blob
.
Since the response will be a blob you don't have to create one.
Also, did you forget to add your auth header to the request?
var resolved = function (url) {
var showFile = function (blob) {
const data = window.URL.createObjectURL(blob);
var link = document.createElement('a');
link.href = data;
link.download = options.name;
link.click();
setTimeout(function () {
window.URL.revokeObjectURL(data);
}, 100)
}
var jwtToken = localStorage.getItem("jwtToken");
var headerObj = {"Authorization": "Bearer " + jwtToken}
var xhr = new XMLHttpRequest();
$.ajax({
xhrFields: {
responseType: 'blob'
}
headers: headerObj,
type:'GET',
url:url
}).done(function(blob){
showFile(blob);
});
};
本文标签: javascriptAJAX response need to be converted to blobStack Overflow
版权声明:本文标题:javascript - AJAX response need to be converted to blob - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742065994a2418837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论