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

后端 未结 13 1116
别跟我提以往
别跟我提以往 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:52

    The correct type is application/pdf for PDF, not application/force-download. This looks like a hack for some legacy browsers. Always use the correct mimetype if you can.

    If you have control over the server code:

    • Forced download/prompt: use header("Content-Disposition", "attachment; filename=myfilename.myextension");
    • Browser tries to open it: use header("Content-Disposition", "inline; filename=myfilename.myextension");

    No control over the server code:

    • Use the HTML5 download attribute. It uses the custom filename specified on the view side.

    NOTE: I prefer setting the filename on the server side as you may have more information and can use common code.

    0 讨论(0)
  • 2020-11-22 04:54

    Here is another method of forcing a file to view in the browser in PHP:

    $extension = pathinfo($file_name, PATHINFO_EXTENSION);
    $url = 'uploads/'.$file_name;
            echo '<html>'
                    .header('Content-Type: application/'.$extension).'<br>'
                    .header('Content-Disposition: inline; filename="'.$file_name.'"').'<br>'
                    .'<body>'
                    .'<object   style="overflow: hidden; height: 100%;
                 width: 100%; position: absolute;" height="100%" width="100%" data="'.$url.'" type="application/'.$extension.'">
                        <embed src="'.$url.'" type="application/'.$extension.'" />
                 </object>'
                .'</body>'
                . '</html>';
    
    0 讨论(0)
  • 2020-11-22 04:55

    This is for ASP.NET MVC

    In your cshtml page:

    <section>
        <h4><a href="@Url.Action("Download", "Document", new { id = @Model.GUID })"><i class="fa fa-download"></i> @Model.Name</a></h4>
        <object data="@Url.Action("View", "Document", new { id = @Model.GUID })" type="application/pdf" width="100%" height="800" class="col-md-12">
            <h2>Your browser does not support viewing PDFs, click on the link above to download the document.</h2>
        </object>
    </section>
    

    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");
        }
    
    0 讨论(0)
  • 2020-11-22 04:57

    If you have Apache add this to the .htaccess file:

    <FilesMatch "\.(?i:pdf)$">
        ForceType application/octet-stream
        Header set Content-Disposition attachment
    </FilesMatch>
    
    0 讨论(0)
  • 2020-11-22 05:03

    If you link to a .PDF it will open in the browser.
    If the box is unchecked it should link to a .zip to force the download.

    If a .zip is not an option, then use headers in PHP to force the download

    header('Content-Type: application/force-download'); 
    header('Content-Description: File Transfer'); 
    
    0 讨论(0)
  • 2020-11-22 05:07

    Oops, there were typing errors in my previous post.

        header("Content-Type: application/force-download");
        header("Content-type: application/pdf");
        header("Content-Disposition: inline; filename=\"".$name."\";");
    

    If you don't want the browser to prompt the user then use "inline" for the third string instead of "attachment". Inline works very well. The PDF display immediately without asking the user to click on Open. I've used "attachment" and this will prompt the user for Open, Save. I've tried to change the browser setting nut it doesn't prevent the prompt.

    0 讨论(0)
提交回复
热议问题