接收zip文件,angularJs [英] Receive zip file, angularJs

查看:150
本文介绍了接收zip文件,angularJs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,当我想从一个REST API下载一个压缩文件,

I've got a problem when I want to download a zip file from a Rest api,

在zip文件是从我的服务器tranfered(含球衣),我收到损坏,...

When the zip file is tranfered from my server (with jersey), I receive it corrupted, ...

我已经试图把的responseType:arraybuffer我的$ http请求,但它不是固定什么...这里是我的code。

I've already tried to put responseType: 'arraybuffer' on my $http request but it isn't fixing anything... here's my code.

$http.get(uploadUrl, {
           cache: false,
           responseType: 'arraybuffer'
      })
     .success(function (data, $scope) {
         var element = angular.element('<a/>');
         console.debug("data : " + data);
         element.attr({
             href: 'data:application/octet-stream;charset=utf-8,' + encodeURI(data),
             target: '_blank',
             download: fileName
         })[0].click();
      })
     .error(function () {
        console.info("fail on download");
      });
};

感谢您的answerd

thanks for your answerd

吉尔斯BODART

推荐答案

我在过去遇到了这个问题。您需要使用缓存以及并触发另存为对话框的开幕,如下所述:

I encountered this problem in the past. You need to use the Buffer as well and trigger the opening of the "Save As" dialog, as described below:

var url = (...)
var expectedMediaType = (...)

var requestData = (...)
$http.post(url, requestData, {
  params: {
    queryParam: 'queryParamValue'
  },
  headers: {
    'Content-Type': 'application/json',
    'Accept': expectedMediaType
  }
}).then(function (response) {
  var filename = (...)
  openSaveAsDialog(filename, response.data, expectedMediaType);
});

下面是openSaveAsDialog函数的内容:

Here is the content of the openSaveAsDialog function:

function openSaveAsDialog(filename, content, mediaType) {
  var blob = new Blob([content], {type: mediaType});
  saveAs(blob, filename);
}

要使用的saveAs 功能,你需要包含的 https://github.com/eligrey/FileSaver.js 库。要安装它,在你的HTML页面中使用标签脚本只是引用其js文件。

To use the saveAs function, you need to include https://github.com/eligrey/FileSaver.js library. To install it, just reference its js file using a tag script in your HTML page.

<script src="js/FileSaver.js"></script>

我写了一篇博客文章中描述了如何解决这个问题:的的https://templth.word$p$pss.com/2014/11/21/handle-downloads-with-angular/ <​​/A>

I wrote a blog post describing how to fix it: https://templth.wordpress.com/2014/11/21/handle-downloads-with-angular/.

希望它会帮助你,
蒂埃里

Hope it will help you, Thierry

这篇关于接收zip文件,angularJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆