我试图在Chrome中打开一个pdf文件,但在显示过程中似乎卡在了某个地方的中间位置。请协助
Screen shot of Request and Result Headers being sent by application serving the PDF
myController的:
[HttpGet]
[Authorize(Roles = ROLE_VIEW)]
public void GetViewFiles(string attachmentID)
{
AttachmentBO bo = new AttachmentBO(this.CurrentUser);
bo.GetViewFileData(attachmentID);
}
AttachmentBO.cs:
public void GetViewFileData(string attachmentID)
{
List<DownloadFileInfoViewModel> retDownloadFilesInfo = new List<DownloadFileInfoViewModel>();
using (var context = this.GetContext())
{
retDownloadFilesInfo = context.GetfileData(attachmentID);
}
// Clear the content of the response
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + retDownloadFilesInfo[0].FileName);
HttpContext.Current.Response.AddHeader("Content-Length", retDownloadFilesInfo[0].FileSize.ToString());
HttpContext.Current.Response.ContentType = ReturnExtension(retDownloadFilesInfo[0].FileExt.ToLower());
HttpContext.Current.Response.BinaryWrite(retDownloadFilesInfo[0].FileData);
HttpContext.Current.Response.Flush(); // this make stream and without it open
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.End();
}
答案 0 :(得分:3)
您是否考虑过使用简单的FileResult
。
[HttpGet]
public FileResult GetPdf()
{
var file = new FileInfo(Server.MapPath("~/App_Data/sample.pdf"));
Response.Headers.Add("content-disposition", $"inline; filename={file.Name}");
/* Return the file from a path
return File(file.FullName, "application/pdf");
*/
//return the file as binary contents
var contents = System.IO.File.ReadAllBytes(file.FullName);
return File(contents, "application/pdf");
}
现在,FileResult
的密钥是不设置文件名(第三个可选参数),因为它将设置重复的Content-Disposition
标头。这将导致PDF在支持PDF查看的浏览器中显示。
上述方法已在以下浏览器中测试过。
答案 1 :(得分:0)
您的标题使PDF显示内联浏览器看起来正确,但您还需要确保将MIME类型发送为“application / pdf”
它也可能是浏览器中的用户设置,请测试Chrome是否在浏览器中为您打开test PDF。
Chrome会在您点击它们时自动打开PDF。
当文件名包含任何非数字字符时,我也看到了奇怪的行为。
尝试清理文件名:
public void GetViewFileData(string attachmentID)
{
List<DownloadFileInfoViewModel> retDownloadFilesInfo = new List<DownloadFileInfoViewModel>();
using (var context = this.GetContext())
{
retDownloadFilesInfo = context.GetfileData(attachmentID);
}
HttpResponse response = HttpContext.Current.Response;
// Clear the content of the response
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
string filename = retDownloadFilesInfo[0].FileName;
string ext = Path.GetExtension(filename);
filename = new string(Path.GetFileNameWithoutExtension(filename).Where(ch => char.IsLetterOrDigit(ch)).ToArray()) + ext;
response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
response.BinaryWrite(retDownloadFilesInfo[0].FileData);
response.Flush(); // this make stream and without it open
response.End();
}
答案 2 :(得分:0)
;filename
值的Content-Disposition
部分正在将其视为下载。只需将其设置为Content-Disposition: inline
即可按您的意愿行事。拥有文件名意味着它是一个附件。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
答案 3 :(得分:0)
以下是我使用的内容:
[HttpGet]
public FileContentResult GetFile(string path)
{
VerifyAccessOrThrow(path);
var fileName = "example";
//We need to enclose the filename in double quotes for the mighty Firefox
Response.AppendHeader("Content-Disposition", $"inline; filename=\"{fileName}\"");
Response.ContentType = MimeMapping.GetMimeMapping(fileName);
return File(fileContent, MimeMapping.GetMimeMapping(fileName));
}