AngularJs get blob with $http.get and extract type from response

这一生的挚爱 提交于 2020-01-31 07:34:21

问题


I have webApi return me a blob file that I have to display. how can I know what type is the blob that I got? It could be anything, pdf, doc, jpeg etc.

$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);
});

or, how can I open blob in a new window without know the file type?


回答1:


Use response.headers() to get the content type:

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); 
});

Consider using 'blob' as the responseType.

For more information, see MDN XHR responseType.



来源:https://stackoverflow.com/questions/43029208/angularjs-get-blob-with-http-get-and-extract-type-from-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!