我在Angular JS中创建了一个应用程序,用于通过$ http帖子下载Excel工作簿.
在下面的代码中,我以JSON的形式传递信息,并通过角度$ http帖子将其发送到服务器REST Web服务(java).Web服务使用JSON中的信息并生成Excel工作簿.在$ http post的成功主体内的响应中,我在该数据变量中获取二进制数据,但不知道如何转换它并下载为Excel文件.
谁能告诉我一些解决方案,将二进制文件转换为Excel文件并下载?
我的代码如下:
$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) {
});
Run Code Online (Sandbox Code Playgroud) 在AngularJS中,有$http.get动态获取数据.遗憾的是,从官方文档中不易理解如何读取二进制数据(例如,用于图像处理).
默认值get将数据提取为a String(在plunker中查看).这非常麻烦.那么,如何在ArrayBuffer中获取它?(注意,因为XHR2 已经可以了.)
<!DOCTYPE html>
<html>
<head>
<title>Using $http.get to read binary data</title>
</head>
<body ng-app>
<h1>$http to load binary data</h1>
<div ng-controller="FetchCtrl" >
<button ng-click="fetch()">fetch</button><br/>
{{info}}
</div>
<script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
<script>
// Controller
function FetchCtrl($scope, $http) {
// See note 1
$scope.URL = "http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png";
$scope.info = "Click 'fetch' to fetch an image" ;
$scope.fetch = function() {
delete $http.defaults.headers.common['X-Requested-With']; // See note 2
$http.get($scope.URL).
success(function(data) {
$scope.info …Run Code Online (Sandbox Code Playgroud) 我从服务器获得以下PDF流: 
如何在AngularJS中读取此流?我尝试使用以下代码在新窗口中将其作为PDF文件打开:
.success(function(data) {
window.open("data:application/pdf," + escape(data));
});
Run Code Online (Sandbox Code Playgroud)
但我无法在打开的窗口中看到内容.
在我的服务器端,我使用的是ASP.NET MVC Web Api,我使用Crystal报告生成PDF文件并将其导出为PDF格式.代码如下:
[HttpPost]
public HttpResponseMessage SetReport(string name, [FromBody]List<KontoDto> konta)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
var strReportName = "KontoReport.rpt";
var rd = new ReportDocument();
string strPath = HttpContext.Current.Server.MapPath("~/") + "Reports//" + strReportName;
rd.Load(strPath);
rd.SetDataSource(konta);
var tip = ExportFormatType.PortableDocFormat;
var pdf = rd.ExportToStream(tip);
response.Headers.Clear();
response.Content = new StreamContent(pdf);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
Run Code Online (Sandbox Code Playgroud)
我的Javascript代码是:
$scope.xxprint = function () {
console.log($scope.konta);
$http.post('/api/konto/setReport/pdf', $scope.konta, { responseType: 'arraybuffer' })
.success(function (data) {
var file = new Blob([data], { type: 'application/pdf' }); …Run Code Online (Sandbox Code Playgroud) 我想使用从Web服务器下载pdf文件$http.我使用这个效果很好的代码,我的文件只保存为html文件,但是当我打开它时,它会以pdf格式打开但在浏览器中.我在Chrome 36,Firefox 31和Opera 23上进行了测试.
这是我的angularjs代码(基于此代码):
UserService.downloadInvoice(hash).success(function (data, status, headers) {
var filename,
octetStreamMime = "application/octet-stream",
contentType;
// Get the headers
headers = headers();
if (!filename) {
filename = headers["x-filename"] || 'invoice.pdf';
}
// Determine the content type from the header or default to "application/octet-stream"
contentType = headers["content-type"] || octetStreamMime;
if (navigator.msSaveBlob) {
var blob = new Blob([data], { type: contentType });
navigator.msSaveBlob(blob, filename);
} else {
var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL; …Run Code Online (Sandbox Code Playgroud) angularjs ×4
javascript ×2
asp.net ×1
asp.net-mvc ×1
excel ×1
http-post ×1
inputstream ×1
java ×1
json ×1
pdf ×1
report ×1