通过节能服务返回一个PDF节能、PDF

2023-09-13 03:11:48 作者:乐观而坚强

我使用 FileSaver.js 并的 Blob.js 成角JS应用程序,以节省通过REST服务(返回字节数组重新presenting文件返回PDF )。

I'm using FileSaver.js and Blob.js into an Angular JS application to save a PDF returned by a REST service (which returns an array of bytes representing the file).

var headers = {headers: {"Authorization":"Bearer "+token, "Accept":"application/pdf"}};

  $http.get(URL, headers)
    .success(function (data) {
      var blob = new Blob([data], {type: 'application/pdf'});
      saveAs(blob, 'contract.pdf');
    });

该文件被保存在正确类型和页数是正确的,但它是完全空白。用编辑器打开它,它原来的只包含由服务器返回,喜欢它的截断数据的第一部分。

the file gets saved with the right type and the number of pages is correct, but it's totally blank. Opening it with an editor, it turned out the it contains only the first part of the data returned by the server, like it's truncated.

感谢大家的帮忙!

推荐答案

$ http.get可能未正确处理二进制数据。尝试 $ HTTP({方法:GET,网址:网址,responseTyp的:arraybuffer,...}) (see angularjs)得到一个二进制对象,你可以把数据。

$http.get probably isn't handling binary data correctly. Try $http({method: "GET", url: URL, responseType: "arraybuffer", ...}) (see angularjs) to get a binary object you can put in for data.

您也可以使用的responseType:斑点,所以你甚至不必创建 VAR一滴,但我认为的responseType少浏览器的支持。

You can also use responseType: "blob" so you don't even have to create var blob, but I think that responseType has less browser support.