强制浏览器下载图片使用Javascript窗口打开?

时间:2022-10-22 14:53:59

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:

我正在使用一个小的Javascript函数来调用下载页面:

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

在下载。php页面如下:

$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!

但它不工作。我做错了什么?提前谢谢!

11 个解决方案

#1


23  

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

使用application/octet-stream代替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

如果[content-处置]头用于应用程序/octet-stream内容类型的响应,那么隐含的建议是,用户代理不应该显示响应,而是直接输入' save response as…”对话框。- RFC 2616 - 19.5.1内容处理

#2


19  

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;
    }
}

#3


7  

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

或者您可以对所有映像文件使用.htaccess文件。如果你想强迫浏览器下载你所有的图片(f.e.从表格列表):

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.

这将查找图片文件a试图强制下载到浏览器中。RewriteConds还检查文件exsist.。最后一条规则确保下载仅用于某些文件类型。

#4


4  

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

我还向.htaccess添加了以下内容

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

Not sure if that helps?

不确定这是否有用?

#5


2  

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

如果您在apache服务器上,这非常简单。

  1. Create a file called .htaccess in the directory in which your files exist. For me it was assets/.htaccess.
  2. 在文件存在的目录中创建一个名为.htaccess的文件。对我来说是assets/。htaccess。
  3. 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
  4. 将以下内容添加到文件AddType应用程序/octe -stream .jpg中。您可以为需要的每个文件扩展添加一个新的行。例如AddType应用程序/八进制. pdf
  5. Save your file
  6. 保存你的文件
  7. Clear your browser cache
  8. 清除浏览器缓存
  9. Refresh your browser and enjoy!
  10. 刷新你的浏览器,享受吧!

#6


1  

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?

您尝试过什么浏览器?

#7


1  

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

这是一个按钮,你可以点击在internet explorer下载图片。

<html>
<head>
<title>Your title</title>
</head>
<body>
<form><input type="button" value="Click Me" onClick="window.location.href='image.jpg'"></form>
</body>
</html>

#8


0  

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).

浏览器可以识别jpg URL,并像.htm一样将它们传递给用户,所以我不认为你可以强制用户端使用它(不过,这不是肯定的)。

Read this

读到这

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

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

#9


0  

See if this helps:

看看这有助于:

Webkit and Excel file(PHPexcel)

Webkit和Excel文件(PHPexcel)

#10


0  

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

#11


0  

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

有一种方法可以强制某个目录中的所有文件被提示进行下载。

Requirement

要求

  1. Apache
  2. Apache
  3. mod_headers enabled
  4. mod_headers启用

In your .htaccess or Apache configuration put the following.

在您的.htaccess或Apache配置中,将如下所示。

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

#1


23  

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

使用application/octet-stream代替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

如果[content-处置]头用于应用程序/octet-stream内容类型的响应,那么隐含的建议是,用户代理不应该显示响应,而是直接输入' save response as…”对话框。- RFC 2616 - 19.5.1内容处理

#2


19  

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;
    }
}

#3


7  

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

或者您可以对所有映像文件使用.htaccess文件。如果你想强迫浏览器下载你所有的图片(f.e.从表格列表):

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.

这将查找图片文件a试图强制下载到浏览器中。RewriteConds还检查文件exsist.。最后一条规则确保下载仅用于某些文件类型。

#4


4  

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

我还向.htaccess添加了以下内容

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

Not sure if that helps?

不确定这是否有用?

#5


2  

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

如果您在apache服务器上,这非常简单。

  1. Create a file called .htaccess in the directory in which your files exist. For me it was assets/.htaccess.
  2. 在文件存在的目录中创建一个名为.htaccess的文件。对我来说是assets/。htaccess。
  3. 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
  4. 将以下内容添加到文件AddType应用程序/octe -stream .jpg中。您可以为需要的每个文件扩展添加一个新的行。例如AddType应用程序/八进制. pdf
  5. Save your file
  6. 保存你的文件
  7. Clear your browser cache
  8. 清除浏览器缓存
  9. Refresh your browser and enjoy!
  10. 刷新你的浏览器,享受吧!

#6


1  

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?

您尝试过什么浏览器?

#7


1  

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

这是一个按钮,你可以点击在internet explorer下载图片。

<html>
<head>
<title>Your title</title>
</head>
<body>
<form><input type="button" value="Click Me" onClick="window.location.href='image.jpg'"></form>
</body>
</html>

#8


0  

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).

浏览器可以识别jpg URL,并像.htm一样将它们传递给用户,所以我不认为你可以强制用户端使用它(不过,这不是肯定的)。

Read this

读到这

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

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

#9


0  

See if this helps:

看看这有助于:

Webkit and Excel file(PHPexcel)

Webkit和Excel文件(PHPexcel)

#10


0  

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

#11


0  

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

有一种方法可以强制某个目录中的所有文件被提示进行下载。

Requirement

要求

  1. Apache
  2. Apache
  3. mod_headers enabled
  4. mod_headers启用

In your .htaccess or Apache configuration put the following.

在您的.htaccess或Apache配置中,将如下所示。

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