Download File Using Javascript/jQuery

前端 未结 28 2925
悲&欢浪女
悲&欢浪女 2020-11-21 05:11

I have a very similar requirement specified here.

I need to have the user\'s browser start a download manually when $(\'a#someID\').click();

But

相关标签:
28条回答
  • 2020-11-21 05:42

    I recommend using the download attribute for download instead of jQuery:

    <a href="your_link" download> file_name </a>
    

    This will download your file, without opening it.

    0 讨论(0)
  • 2020-11-21 05:43

    There are so many little things that can happen when trying to download a file. The inconsistency between browsers alone is a nightmare. I ended up using this great little library. https://github.com/rndme/download

    Nice thing about it is that its flexible for not only urls but for client side data you want to download.

    1. text string
    2. text dataURL
    3. text blob
    4. text arrays
    5. html string
    6. html blob
    7. ajax callback
    8. binary files
    0 讨论(0)
  • 2020-11-21 05:44

    Simple example using an iframe

    function downloadURL(url) {
        var hiddenIFrameID = 'hiddenDownloader',
            iframe = document.getElementById(hiddenIFrameID);
        if (iframe === null) {
            iframe = document.createElement('iframe');
            iframe.id = hiddenIFrameID;
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
        }
        iframe.src = url;
    };
    

    Then just call the function wherever you want:

    downloadURL('path/to/my/file');

    0 讨论(0)
  • 2020-11-21 05:44

    for me this is working ok tested in chrome v72

    function down_file(url,name){
    var a = $("<a>")
        .attr("href", url)
        .attr("download", name)
        .appendTo("body");
    a[0].click();
    a.remove();
    }
    
    down_file('https://www.useotools.com/uploads/nulogo[1].png','logo.png')
    
    0 讨论(0)
  • 2020-11-21 05:44

    The answer submitted by hitesh on Dec 30 '13 does in fact work. It just requires a little adjusting:

    The PHP file can call itself. In other words, just create a file named saveAs.php, and put this code into it...

            <a href="saveAs.php?file_source=YourDataFile.pdf">Download pdf here</a>
    
        <?php
            if (isset($_GET['file_source'])) {
                $fullPath = $_GET['file_source'];
                if($fullPath) {
                    $fsize = filesize($fullPath);
                    $path_parts = pathinfo($fullPath);
                    $ext = strtolower($path_parts["extension"]);
                    switch ($ext) {
                        case "pdf":
                        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
                        header("Content-type: application/pdf"); // add here more headers for diff. extensions
                        break;
                        default;
                        header("Content-type: application/octet-stream");
                        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
                    }
                    if($fsize) {//checking if file size exist
                      header("Content-length: $fsize");
                    }
                    readfile($fullPath);
                    exit;
                }
            }
        ?>
    
    0 讨论(0)
  • 2020-11-21 05:45

    Use an invisible <iframe>:

    <iframe id="my_iframe" style="display:none;"></iframe>
    <script>
    function Download(url) {
        document.getElementById('my_iframe').src = url;
    };
    </script>
    

    To force the browser to download a file it would otherwise be capable of rendering (such as HTML or text files), you need the server to set the file's MIME Type to a nonsensical value, such as application/x-please-download-me or alternatively application/octet-stream, which is used for arbitrary binary data.

    If you only want to open it in a new tab, the only way to do this is for the user to a click on a link with its target attribute set to _blank.

    In jQuery:

    $('a#someID').attr({target: '_blank', 
                        href  : 'http://localhost/directory/file.pdf'});
    

    Whenever that link is clicked, it will download the file in a new tab/window.

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