Force Browser to download Image with Javascript window.open?

前端 未结 11 815
感动是毒
感动是毒 2020-11-27 19:37

Is there a way to make an image a download once you click on it (without right-click save image as)?

I\'m using a small Javascript function to call the do

相关标签:
11条回答
  • 2020-11-27 19:52

    If you're on an apache server this is really simple.

    1. Create a file called .htaccess in the directory in which your files exist. For me it was assets/.htaccess.
    2. Add the following to the file AddType application/octet-stream .jpg. You can add a new line for each file extension that you need. E.g. AddType application/octet-stream .pdf
    3. Save your file
    4. Clear your browser cache
    5. Refresh your browser and enjoy!
    0 讨论(0)
  • 2020-11-27 19:52

    Browsers recognize jpg URL's and hand them over just like a .htm, so I don't think you can force it on the user-end (not positive on that, though).

    Read this

    http://apptools.com/phptools/force-download.php

    0 讨论(0)
  • 2020-11-27 19:52

    Try to change this:

    header("Content-disposition: attachment; filename= ".$file."");
    

    To:

    header("Content-disposition: attachment; filename= ".$file);
    

    If the above doesn't work to you, here a function to force a file to be downloadable:

        <?php
    function downloadFile($file, $type)
    {
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: Content-type: $type");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($file));
    readfile($file);
    }
    
        downloadFile("sd.jpg", "image/jpg");
        ?>
    
    0 讨论(0)
  • 2020-11-27 19:54

    See if this helps:

    Webkit and Excel file(PHPexcel)

    0 讨论(0)
  • 2020-11-27 19:55

    This worked for me

    header("Pragma: public");
    header('Content-disposition: attachment; filename='.$title);
    header("Content-type: ".mime_content_type($sample_file));
    header('Content-Transfer-Encoding: binary');
    ob_clean(); 
    flush(); 
    readfile($sample_file);
    

    I also added the following to .htaccess

    SetEnvIf Request_URI "\.jpg$" requested_jpg=jpg
    Header add Content-Disposition "attachment" env=requested_jpg
    

    Not sure if that helps?

    0 讨论(0)
  • 2020-11-27 19:55

    This is a button you can click on in internet explorer to download pic

    <html>
    <head>
    <title>Your title</title>
    </head>
    <body>
    <form><input type="button" value="Click Me" onClick="window.location.href='image.jpg'"></form>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题