从angular js下载图像

时间:2017-05-26 16:42:39

标签: angularjs web

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

从这段代码我得到的对象:      $ scope.clickEvent = function(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);


                });
            };

我要下载的东西是: enter image description here

1 个答案:

答案 0 :(得分: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' 
    })