首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使文件在浏览器中打开,而不是使用CHROME & FireBox下载

使文件在浏览器中打开,而不是使用CHROME & FireBox下载
EN

Stack Overflow用户
提问于 2017-07-13 08:02:57
回答 4查看 4.9K关注 0票数 1

我试图让一个pdf文件在chrome中打开,但它似乎在显示过程中卡在了某个地方。请协助

Screen shot of Request and Result Headers being sent by application serving the PDF

MyController:

代码语言:javascript
复制
    [HttpGet]
    [Authorize(Roles = ROLE_VIEW)]
    public void GetViewFiles(string attachmentID)
    {
        AttachmentBO bo = new AttachmentBO(this.CurrentUser);
        bo.GetViewFileData(attachmentID);

    }

AttachmentBO.cs:

代码语言:javascript
复制
   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();
    }   
EN

回答 4

Stack Overflow用户

发布于 2017-07-13 08:23:27

你有没有想过只使用一个简单的FileResult

代码语言:javascript
复制
[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查看的浏览器中显示。

上述方法已经在以下浏览器中进行了测试。

微软54.0.1

  • Internet 40.15063.0.0

  1. Chrome Version 59.0.3071.115
  2. Firefox 54.0.1
  3. Internet Explorer 11
票数 3
EN

Stack Overflow用户

发布于 2017-07-13 08:19:22

为了使PDF在浏览器中的内联显示看起来正确,您还需要确保MIME类型作为"application/pdf“发送

这也可能是浏览器中的用户设置,请测试一下Chrome是否在浏览器中为您打开此test PDF

Open PDFs in Chrome

  • 在您的电脑上打开Chrome。在右上角,单击More More,然后单击
  • 。高级
  • 在底部,单击

  • 在“隐私和安全”下,单击“内容设置”。在靠近底部的位置,单击

documents

  • 使用其他application.

关闭打开PDF

现在,当你点击PDF时,Chrome会自动打开它们。

当文件名包含任何非数字字符时,我也看到了奇怪的行为。

尝试清除您的文件名:

代码语言:javascript
复制
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();
}   
票数 0
EN

Stack Overflow用户

发布于 2017-07-13 08:20:59

Content-Disposition值的;filename部分使其将其视为下载。只需将其设置为Content-Disposition: inline,它就会像您所希望的那样运行。有一个文件名意味着它是一个附件。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45069386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档