Force to open “Save As…” popup open at text link click for PDF in HTML

后端 未结 18 1165
無奈伤痛
無奈伤痛 2020-11-22 10:28

I have some big size PDF catalogs at my website, and I need to link these as download. When I googled, I found such a thing noted below. It should open the \"Save As

相关标签:
18条回答
  • 2020-11-22 10:58

    I found a very simple solution for Firefox (only works with a relative rather than a direct href): add type="application/octet-stream":

    <a href="./file.pdf" id='example' type="application/octet-stream">Example</a>
    
    0 讨论(0)
  • 2020-11-22 10:59

    Meta tags are not a reliable way to achieve this result. Generally you shouldn't even do this - it should be left up to the user/user agent to decide what do to with the content you provide. The user can always force their browser to download the file if they wish to.

    If you still want to force the browser to download the file, modify the HTTP headers directly. Here's a PHP code example:

    $path = "path/to/file.pdf";
    $filename = "file.pdf";
    header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
    header('Accept-Ranges: bytes');  // Allow support for download resume
    header('Content-Length: ' . filesize($path));  // File size
    header('Content-Encoding: none');
    header('Content-Type: application/pdf');  // Change the mime type if the file is not PDF
    header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
    readfile($path);  // This is necessary in order to get it to actually download the file, otherwise it will be 0Kb
    

    Note that this is just an extension to the HTTP protocol; some browsers might ignore it anyway.

    0 讨论(0)
  • 2020-11-22 11:00

    With large PDF files the browser hangs. In Mozilla, menu ToolsOptionsApplications, then next to the content type Adobe Acrobat document. In the Action drop down, select Always ask.

    This did not work for me, so what worked was:

    Menu Tools* → Add-onsAdobe Acrobat (Adobe PDF plugin for Firefox) → DISABLE. Now I am able to download e-books!

    0 讨论(0)
  • 2020-11-22 11:01

    Generally it happens, because some browsers settings or plug-ins directly open PDF in the same window like a simple web page.

    The following might help you. I have done it in PHP a few years back. But currently I'm not working on that platform.

    <?php
        if (isset($_GET['file'])) {
            $file = $_GET['file'];
            if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
                header('Content-type: application/pdf');
                header("Content-Disposition: attachment; filename=\"$file\"");
                readfile($file);
            }
        }
        else {
            header("HTTP/1.0 404 Not Found");
            echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
        }
    ?>
    

    Save the above as download.php.

    Save this little snippet as a PHP file somewhere on your server and you can use it to make a file download in the browser, rather than display directly. If you want to serve files other than PDF, remove or edit line 5.

    You can use it like so:

    Add the following link to your HTML file.

    <a href="download.php?file=my_pdf_file.pdf">Download the cool PDF.</a>
    

    Reference from: This blog

    0 讨论(0)
  • 2020-11-22 11:03

    After the file name in the HTML code I add ?forcedownload=1

    This has been the simplest way for me to trigger a dialog box to save or download.

    0 讨论(0)
  • 2020-11-22 11:04
    <a href="file link" download target="_blank">Click here to download</a>
    

    It works for me in Firefox and Chrome.

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