AngularJS+Jersey下载excel
2017-02-21 10:34
573 查看
AngularJS代码:
$scope.testDownload =function () {
$http.post("rest/excel/down", $scope.req, {responseType:'arraybuffer'}).success(function
(data) {
varblob =new
Blob([data], {type:"application/vnd.ms-excel"});
varfileName ="ttt2.xls";
vara = document.createElement("a");
document.body.appendChild(a);
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.click();
}).error(function (data) {
$("#serverErrorModal").modal({show:true});
});
}
注意post的方法里要加responseType: 'arraybuffer'参数,不然下载的excel会乱码!!!
使用{type:"application/vnd.ms-excel"}的写法,可以保存为xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”则会保存为xlsx。
Server端示例代码:
@POST
@Path("down")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
publicbyte[]
downloadExcel(@Context HttpServletResponse response)throws IOException
{
//.....略
File file1=
new File("D:/excel/ttt2.xls");
byte[] aa = Files.toByteArray(file1);
return aa;
}
$scope.testDownload =function () {
$http.post("rest/excel/down", $scope.req, {responseType:'arraybuffer'}).success(function
(data) {
varblob =new
Blob([data], {type:"application/vnd.ms-excel"});
varfileName ="ttt2.xls";
vara = document.createElement("a");
document.body.appendChild(a);
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.click();
}).error(function (data) {
$("#serverErrorModal").modal({show:true});
});
}
注意post的方法里要加responseType: 'arraybuffer'参数,不然下载的excel会乱码!!!
使用{type:"application/vnd.ms-excel"}的写法,可以保存为xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”则会保存为xlsx。
Server端示例代码:
@POST
@Path("down")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
publicbyte[]
downloadExcel(@Context HttpServletResponse response)throws IOException
{
//.....略
File file1=
new File("D:/excel/ttt2.xls");
byte[] aa = Files.toByteArray(file1);
return aa;
}
相关文章推荐
- JS导出excel设置下载的标题/与angular结合冲突
- AngularJS + Jersey REST 文件下载
- AngularJs + REST API 文件下载
- 基于 angularjs+jfinal 的 excel文件导出
- js 下载excel
- webAPI+angularJS文件上传和下载
- POST+JSON+JS下载Excel或CSV
- JS下载EXT中指定grid中的数据到EXCEL中
- angular2 把请求到的json文件下载成Excel
- angular.js+node.js实现下载图片处理详解
- SSM/angularjs _ 文件的上传下载
- SpringBoot+AngularJS导出数据库数据到excel
- angular get/post 下载 excel
- js 导出excel,设置下载的标题
- 【MVC】AngularJs+KendoUI开发报表Demo(导出Excel和折线图)
- angular下载二进制Excel文件
- Gradle+Jersey+AngularJs图片上传功能后台代码
- Spring Boot+AngularJS导出excel
- Spring Boot+AngularJS导出excel
- 利用Ajax向Servlet发送请求下载统计后的Excel文件,利用jquery.blockUI.js给予用户等待提示