【问题标题】:HTML5 download attribution not working for file without extensionHTML5 下载属性不适用于没有扩展名的文件
【发布时间】:2014-08-26 03:49:08
【问题描述】:

我正在尝试使用 a href 的“下载”属性来重命名没有扩展名的文件,但它不起作用。

<a href="/fileWithoutExtension" download="newName.pdf">

我尝试过使用 IE、Chrome 和 FF。

有什么想法吗?

谢谢,

【问题讨论】:

    标签: html download href mime-types file-extension


    【解决方案1】:

    下载属性仅在 Chrome 14+ 和 Firefox 20+ 中有效,因此这可能解释了为什么您无法使其正常工作。我建议你不要使用它,因为它还没有被广泛使用。更喜欢其他一些替代品。

    您可以使用此 javascript 代码检查您的属性是否受支持,取自此处:http://www.webdesignerdepot.com/2013/04/how-to-use-the-download-attribute/

    var a = document.createElement('a');
    
    if(typeof a.download != "undefined")
    {
       // download attribute is supported
    }
    else
    {
      // download attribute is not supported
    }
    

    如果您想更改文件名,只需使用服务器端 PHP 脚本:

    <?php
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="newFile.pdf"');
    echo file_get_contents("your_original_file");
    ?>
    

    让你的链接像这样

    <a href="file.php"></a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-05
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多