AngularJS $ HTTP-POST - 二进制转换为Excel文件下载 [英] AngularJS $http-post - convert binary to excel file and download

查看:237
本文介绍了AngularJS $ HTTP-POST - 二进制转换为Excel文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个角JS应用程序通过$ HTTP POST下载一个Excel工作簿。

在低于code,我传递JSON形式的信息,并通过角$ HTTP POST发送到服务器REST Web服务(Java)。 Web服务使用从JSON的信息,并产生一个Excel工作簿。在$成功机身HTTP POST中的反应,我认为数据变量中获取的二进制数据,但不知道如何将它转换和下载为Excel文件。

谁能告诉我一些这方面的解决方案,用于将二进制Excel文件并下载?

作为如下

我的code:

  $ HTTP({
        网址:'myweb.com/myrestService',
        方法:POST,
        数据:JSON,//这是您的JSON数据串
        标题:{
           内容类型:应用/ JSON
        }
    })。成功(功能(数据,状态,头,配置){        //这里我得到的Excel工作表的二进制DATAS在数据    })错误(功能(数据,状态,头,配置){    });


解决方案

只注意到你不能使用它,因为IE8 / 9,但我会推提交反正...也许有人认为它有用

这实际上可以通过浏览器来完成,使用一滴。注意的responseType ,并在成功code 的承诺。

  $ HTTP({
    网址:'你的/ web服务',
    方法:POST,
    数据:JSON,//这是您的JSON数据串
    标题:{
       内容类型:应用/ JSON
    },
    responseTyp的:arraybuffer
})。成功(功能(数据,状态,头,配置){
    VAR BLOB =新的Blob([数据] {类型:应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet});
    VAR的ObjectURL = URL.createObjectURL(BLOB);
    window.open(的ObjectURL);
})错误(功能(数据,状态,头,配置){
    //上传失败
});

有一些问题,它虽然这样的:


  1. 它不支持 IE 8和9

  2. 它打开一个弹出窗口,打开的ObjectURL ,人们可能会阻止

  3. 生成怪异的文件名

它的工作!

在PHP服务器端code我看起来像这样测试这一点。我敢肯定,你可以在Java中设置类似的标题:

  $文件=file.xlsx;
标题(内容处置:附件;文件名='$文件);
标题(内容长度:文件大小($文件));
标题(内容传输编码:二进制');
标题(缓存控制:必重新验证');
头('杂注:公开');
回声json_en code(ReadFile的($文件));

I've created an application in Angular JS for downloading an Excel workbook through $http post.

In the below code I'm passing the information in the form of JSON , and send it to the server REST web service (java) through an angular $http post. The web service uses the information from the JSON and produces an Excel workbook. In the response within the success body of $http post, I'm getting binary data within that data variable, but don't know how to convert it and download as an Excel file.

Can anyone please tell me some solution for this for converting the binary to Excel file and download?

My code is as given below:

$http({
        url: 'myweb.com/myrestService',
        method: "POST",
        data: json, //this is your json data string
        headers: {
           'Content-type': 'application/json'
        }
    }).success(function (data, status, headers, config) {

        // Here i'm getting excel sheet binary datas in 'data' 

    }).error(function (data, status, headers, config) {

    });

解决方案

Just noticed you can't use it because of IE8/9 but I'll push submit anyway... maybe someone finds it useful

This can actually be done through the browser, using blob. Notice the responseType and the code in the success promise.

$http({
    url: 'your/webservice',
    method: "POST",
    data: json, //this is your json data string
    headers: {
       'Content-type': 'application/json'
    },
    responseType: 'arraybuffer'
}).success(function (data, status, headers, config) {
    var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
    var objectUrl = URL.createObjectURL(blob);
    window.open(objectUrl);
}).error(function (data, status, headers, config) {
    //upload failed
});

There are some problems with it though like:

  1. It doesn't support IE 8 and 9:
  2. It opens a pop up window to open the objectUrl which people might have blocked
  3. Generates weird filenames

It did work! The server side code in PHP I tested this with looks like this. I'm sure you can set similar headers in Java:

$file = "file.xlsx";
header('Content-disposition: attachment; filename='.$file);
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo json_encode(readfile($file));

这篇关于AngularJS $ HTTP-POST - 二进制转换为Excel文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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