问题描述
在我的服务器端我使用ASP.NET MVC的Web的API,我在哪里产生与Crystal报表PDF文件并将其导出为PDF格式。在code去如下:
On my server side I am using ASP.NET MVC Web Api, where I am generating the PDF file with Crystal report and exporting it to PDF format. The code goes as follows:
[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;
}
我的JavaScript code是:
My Javascript code is:
$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' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
});
};
这根本不起作用。我不知道有什么不对的code。我得到的浏览器中打开PDF浏览器,但它是空的。此外,创建的PDF正确地创建,我可以将它保存到磁盘,然后打开它,然后使用Adobe Acrobat查看。该型Htt presponseMessage的内容似乎也是正确的通过的提琴手观察。见图片:
This simply does not work. I don't know what's wrong with this code. I get the browser to open the pdf viewer, but it is empty. Also, the created pdf is correctly created as I can save it to disk and open it then with Adobe Acrobat viewer. The content of the HttpResponseMessage seems also correct viewed via Fiddler. See image:
推荐答案
好像我做它正确的所有时间。问题是我angularjs版(V1.08)。当升级到V1.2一切工作正常。在V1.08的的responseType:arraybuffer
paramater(这是我在做什么重要的)是简单地通过angularjs忽略。这似乎是实现为V.1.1的。见这太问题:How在一个ArrayBuffer AngularJS读取二进制数据?
Seems I did it correctly all the time. The problem was with my angularjs version (v1.08). When upgrading to v1.2 everything worked ok. In v1.08 the responseType: 'arraybuffer'
paramater (which is crucial to what I was doing) was simply ignored by angularjs. It seems to be implemented as of v.1.1. See this SO question: How to read binary data in AngularJS in an ArrayBuffer?
这篇关于如何显示服务器端生成的JavaScript PDF流通过HttpMessageResponse内容发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!