简体   繁体   中英

Force Browser to download Image with Javascript window.open?

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 download page:

<a href="#" 
   onclick="window.open('download.php?file=test.jpg', 'download', 'status=0');"
>Click to download</a>

In the download.php page I have something like:

$file = $_GET['file'];
header('Content-Description: File Transfer');
header("Content-type: image/jpg");
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);

But it doesn't work. What am I doing wrong?
Thanks in advance!

Use application/octet-stream instead of image/jpg :

If [the Content-Disposition] header is used in a response with the application/octet-stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.
RFC 2616 – 19.5.1 Content-Disposition

I think you forgot to add Path on the header

if(isset($_GET['file'])){
    //Please give the Path like this
    $file = 'images/'.$_GET['file'];

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
}

Or you can use .htaccess file for all your image files. In case you want to force the browser to download all your images (fe from a table list):

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^download$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .(jpe?g|gif|png)$ index.php?file=noFoundFilePage [L,NC]
RewriteCond %{QUERY_STRING} ^download$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .(jpe?g|gif|png)$ - [L,NC,T=application/octet-stream] 

This looks for image files a tries to force download them into the browser. The -f RewriteConds also checks that the file exsist.. The last rule ensures that download is used only for certain file types.

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?

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. Eg AddType application/octet-stream .pdf
  3. Save your file
  4. Clear your browser cache
  5. Refresh your browser and enjoy!

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>

Once you've added the Content-Disposition: attachment header, you should be able to use a normal link:

<a href="download.php?file=test.jpg">Click to download</a>

What browsers have you tried this with?

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

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");
    ?>

See if this helps:

Webkit and Excel file(PHPexcel)

Here's a way to force all files in a certain directory to be prompted for a download.

Requirement

  1. Apache
  2. mod_headers enabled

In your .htaccess or Apache configuration put the following.

<Directory /path/to/downloadable/files>
    Header set Content-Disposition attachment
    Header set Content-Type application/octet-stream
</Directory>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM