我尝试在Firefox(v.49.0.2)浏览器中保存(CTRL + S)正确显示的PDF文档时,我的客户无法下载PDF文档。 我不知道这是我的编程问题还是浏览器问题。 我可以下载的唯一方法是点击PDF插件的“下载”按钮,但我的客户想要用(CTRL + S)选项保存文件。 请看一下这张照片:
还有一个角度代码,我尝试在浏览器中打开文件:它适用于Chrome和Edge,它还可以在Firefox中打开PDF。响应对象是$ http响应。
function openFile(response) {
var responseHeaders = response.headers();
var contentType = responseHeaders['content-type'];
var contentDisposition = responseHeaders['content-disposition'];
var fileName = contentDisposition.match(/filename="(.+)"/)[1];
fileName = fileName.substring(0, fileName.indexOf(';')-1);
var file = new Blob([response.data], { type: contentType });
if(contentType==='application/pdf') //YES content-type is PDF
{
try
{
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
catch(err) //For Edge, just save a file
{
FileSaver.saveAs(file, fileName);
}
}
else //for other content types, just save a file
{
FileSaver.saveAs(file, fileName);
}
}
这是我的C#后端代码:
byte[] report = service.GetReportCustomerCreditRatesCard();//render report
RenderFormatResolver renderResolver = new RenderFormatResolver(request.renderFormat);
HttpContent content = new ByteArrayContent(report);
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = content;
response.Content.Headers.ContentType = new MediaTypeHeaderValue(renderResolver.MIMEType);
response.Content.Headers.ContentLength = report.Length;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") //"attachment", "inline"
{
FileName = String.Format("{0}." + renderResolver.FileNameExtension,
Translations.REPORT_FILENAME_CUSTOMER_CARD),
Name = Translations.REPORT_FILENAME_CUSTOMER_CARD
};
return response;
答案 0 :(得分:0)
尝试一次更改响应对象中的内容处置标头以保存文件名:
Content-Disposition: attachment; filename="document.pdf"
类似于:
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue( 'attachment; filename="document.pdf"' );
这可能会有所帮助。不确定,但值得一试......