我正在尝试使用javascript将从WebApi收到的PDF流显示给浏览器。 Currennt代码在Chrome上正常运行。但它在IE 10中没有显示任何内容(没有错误)。请问。
谢谢,
服务器正在ASP.NET WebApi上运行。基本上只是从报告模板导出PDF流并发送回客户端。
[Route("api/saleInvoices/getReport")]
public IHttpActionResult GetReport(int invoiceID)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
//report to pdf stream
SaleInvoiceReport report = new SaleInvoiceReport(invoiceID);
report.CreateDocument();
//PdfExportOptions opts = new PdfExportOptions();
//opts.ShowPrintDialogOnOpen = true;
//report.ExportToPdf(ms, opts);
report.ExportToPdf(ms);
ms.Seek(0, SeekOrigin.Begin);
byte[] buffer = ms.ToArray();
//return
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(buffer);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
string fileName = string.Format("SalesInvoice_{0}.pdf", invoiceID);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline") { FileName = fileName };
return ResponseMessage(result);
}
}
catch (Exception ex)
{
DatabaseHelper.Log(ex);
return BadRequest(ex.Message);
}
}
客户端正在使用AngularJS服务/控制器/视图。
服务:在bufferarray中接收pdf流。
var _report = function (id) {
//delete $http.defaults.headers.common['X-Requested-With'];
var req={
url: sharedService.getApiUrl() + '/' + bizType + '/getReport?invoiceID=' + id,
method: 'GET',
//headers:{
// 'Authorization': 'Bearer ' + sharedService.getToken(),
//},
//headers: { 'Accept': 'application/pdf' },
responseType:'arraybuffer',
};
return $http(req);
};
控制器:在Blob中托管pdf流并返回其可信URL。
saleInvoiceService.report($scope.invoice.id)
.success(function (response) {
console.log(JSON.stringify(response));
var file = new Blob([response], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
$scope.url = $sce.trustAsResourceUrl(fileURL);
//window.open(fileURL);
//$scope.url = fileURL;
$scope.isLoading = false;
$scope.info = '';
})
.error(function (data) {
$scope.isLoading = false;
$scope.info = JSON.stringify(data);
});
查看:在iframe中指定blob的可信网址。
<iframe ng-src="{{url}}" class="iframe-report"></iframe>