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

后端 未结 1 1015
囚心锁ツ
囚心锁ツ 2021-02-14 04:03

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({ me         


        
1条回答
  •  天涯浪人
    2021-02-14 04:24

    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.

    0 讨论(0)
提交回复
热议问题