我有服务器端pdf生成,为此,我正在使用c#读取数据并使用iTectSharp生成pdf。生成pdf后,我将响应返回到UI,并且在UI上,JavaScript应下载pdf文件。但是Java脚本代码无法下载pdf。
API请求响应标头如下
Cache-Control: no-cache
Content-Disposition: attachment; filename=ReportSummary.pdf
Content-Length: 84595
Content-Type: application/pdf
Date: Wed, 13 Feb 2019 05:11:03 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
在服务器上获取API的代码
using (var workStream = new MemoryStream())
{
using (var pdfWriter = new PdfWriter(workStream))
{
ConverterProperties props = new ConverterProperties();
HtmlConverter.ConvertToPdf(contents, pdfWriter,props);
byte[] byteInfo = workStream.ToArray();
var res = new HttpResponseMessage(HttpStatusCode.OK);
res.Content = new ByteArrayContent(byteInfo);
res.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "BenchmarkSummary.pdf"
};
return res;
}
}
前端API的代码
Service.getReport(values).then(response => {
var blob=new Blob([response.data]);
const link = this.linkRef.current;
link.href= window.URL.createObjectURL(blob);
link.download="Report.pdf";
link.click();
})