Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
html下载标签_Html - Fatal编程技术网

html下载标签

html下载标签,html,Html,基本问题是:我有一个指向图像文件的链接。当我点击链接时,所需的行为是获得下载对话框,这将允许我启动相关的图像编辑器。这不会发生,因为图像文件是由浏览器渲染的 当用户点击链接时,我是否可以使用html魔法强制浏览器提供下载对话框 非常感谢您提供任何帮助或指导。仅使用HTML是不可能的,但是您可以通过使用服务器端语言(如PHP)修改标题来强制下载文件 // grab the requested file's name $file_name = $_GET['file']; // make sur

基本问题是:我有一个指向图像文件的链接。当我点击链接时,所需的行为是获得下载对话框,这将允许我启动相关的图像编辑器。这不会发生,因为图像文件是由浏览器渲染的

当用户点击链接时,我是否可以使用html魔法强制浏览器提供下载对话框


非常感谢您提供任何帮助或指导。

仅使用HTML是不可能的,但是您可以通过使用服务器端语言(如PHP)修改标题来强制下载文件


// grab the requested file's name
$file_name = $_GET['file'];

// make sure it's a file before doing anything!
if(is_file($file_name))
{

    /*
        Do any processing you'd like here:
        1.  Increment a counter
        2.  Do something with the DB
        3.  Check user permissions
        4.  Anything you want!
    */

    // required for IE
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

    // get the file mime type using the file extension
    switch(strtolower(substr(strrchr($file_name,'.'),1)))
    {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
        default: $mime = 'application/force-download';
    }
    header('Pragma: public');   // required
    header('Expires: 0');       // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
    header('Cache-Control: private',false);
    header('Content-Type: '.$mime);
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file_name));    // provide file size
    header('Connection: close');
    readfile($file_name);       // push it out
    exit();

}

若你们想显示下载对话框,你们需要在你们的图片上加上特殊的标题

在PHP中,它将是:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=image.jpg');
header('Content-Transfer-Encoding: binary');

另一种解决方案是将其添加到包含图像的文件夹中的htaccess文件中:

AddType应用程序/octet-stream.jpg
两种方法:

  • 在锚定标记的
    href
    中使用:

  • 在某些特定浏览器中,如Google Chrome和Firefox,您可以执行以下操作:


  • 但这是否意味着只要导航到此页面,用户就将被迫下载图像?不,您需要做的是更改图像链接,以指向通过$_GET['file']发送的php文件,该文件位于图像所在的位置。然后是的,该文件将被强制下载它的mediawiki,因此我不确定是否可以从mediawiki扩展的上下文轻松地操作http头否,您要做的是将此代码保存为php文件,并将其放置在您的images文件夹中。然后,要强制下载图像,请链接到该文件,如:不需要修改代码,这是否会强制所有jpg文件被视为原始二进制文件?我希望能够在浏览器中呈现图像…是的,它将强制下载所有图像。使用此解决方案,您可以将要强制下载的图像放在单独的文件夹中,并仅将此代码应用于该文件夹。如果出于任何原因无法做到这一点,那么PHP或类似的解决方案是唯一的方法。