How do I force files to open in the browser instead of downloading (PDF)?

后端 未结 13 1264
别跟我提以往
别跟我提以往 2020-11-22 04:15

Is there a way to force PDF files to open in the browser when the option \"Display PDF in browser\" is unchecked?

I tried using the embed tag and an iframe, but it o

13条回答
  •  一整个雨季
    2020-11-22 04:55

    This is for ASP.NET MVC

    In your cshtml page:

    @Model.Name

    Your browser does not support viewing PDFs, click on the link above to download the document.

    In your controller:

    public ActionResult Download(Guid id)
        {
            if (id == Guid.Empty)
                return null;
    
            var model = GetModel(id);
    
            return File(model.FilePath, "application/pdf", model.FileName);
        }
    
    public FileStreamResult View(Guid id)
        {
            if (id == Guid.Empty)
                return null;
    
            var model = GetModel(id);
    
            FileStream fs = new FileStream(model.FilePath, FileMode.Open, FileAccess.Read);
    
            return File(fs, "application/pdf");
        }
    

提交回复
热议问题