我有webApi返回一个我必须显示的blob文件。 我怎么知道我得到的斑点是什么类型的?它可以是任何东西,pdf,doc,jpeg等。
$http({ method: 'GET', url: (itemsUrl), responseType: 'arraybuffer' }).then(function mySucces(res) {
var byteArray = res.data;
byteArray = new Uint8Array(byteArray);
var file = new Blob([byteArray], { type: '??????' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
});
或者,如何在不知道文件类型的情况下在新窗口中打开blob?
答案 0 :(得分:19)
使用response.headers()
获取内容类型:
var config = { responseType: 'blob' };
$http.get(itemsUrl, config).then(function onSuccess(response) {
var blob = response.data;
var contentType = response.headers("content-type");
var fileURL = URL.createObjectURL(blob);
window.open(fileURL);
});
考虑使用'blob'
作为responseType。
有关详细信息,请参阅MDN XHR responseType。