2017-05-26 61 views
0

我从MongoDB获取图像。现在我想通过代码将图像下载到我的系统中。从角度js下载图像

从这个代码我得到的对象: $ scope.clickEvent =功能(X){

   var path = x.path.replace('uploads',''); 
      $http({ 
      method: 'GET', 
        url: $API_URL + path, 
        headers: { 
         "Authorization": $localStorage.currentUser.token 
        } 
       }).then(function (response) { 



       var file = new Blob([response.data], {type: 'application/Image'}); 
        var fileURL = URL.createObjectURL(file); 
        window.open(fileURL); 

        console.log("Result: " + response.data); 


       }); 
      }; 

的Bt我收到下载的东西是: enter image description here

回答

0

尝试添加content-type头部和responseType'blob'

$http({ 
     method: 'GET', 
     url: $API_URL + path, 
     headers: { 
     'Content-Type': 'image/jpg', //content type set to image 
     'Authorization': $localStorage.currentUser.token 
     }, 
     responseType: 'blob' 
    }) 
+0

得到了答案。谢谢。 –