【问题标题】:file is downloading without propername and extension正在下载的文件没有正确的名称和扩展名
【发布时间】:2015-07-04 13:42:01
【问题描述】:

在我的项目中,我有一个下载文件的功能,文件如下所示。当我单击超链接时,它将触发下载,但没有正确的名称和文件扩展名。但在超链接中,文件的确切名称被提及为 angular-treasure-overlay-spinner-master.zip。

谁能告诉我如何按照超链接中指定的确切名称下载文件 (angular-treasure-overlay-spinner-master.zip)

Working Demo

<div class="well text-left">
     <img src="https://api.kandy.io/v1.2/devices/content/thumbnail?key=UAT5b6f2e196b0a4e0793ff17ac44b6404c&amp;content_uuid=7DF8CD44-B614-4C4A-82F3-1C5E36B4CC02&amp;device_id=FF4A88BA0C2840749D5AB91584C29E4D&amp;thumbnail_size=500x500" style="width: 20px;"/>
     <a href="https://api.kandy.io/v1.2/devices/content?key=UAT5b6f2e196b0a4e0793ff17ac44b6404c&amp;content_uuid=7DF8CD44-B614-4C4A-82F3-1C5E36B4CC02&amp;device_id=FF4A88BA0C2840749D5AB91584C29E4D" target="_blank">angular-treasure-overlay-spinner-master.zip</a>
</div>

【问题讨论】:

  • “超链接”文本与“超链接”文本不同
  • 对不起....这里是 jsfiddle - jsfiddle.net/jxk90pue
  • 我没有收到这个"hyperlink" text is not the same as the "hyperlink"
  • 我们怎样才能得到那个名称和扩展名,如果我们下载文件并将扩展名更改为 .zip,我就能得到所有内容

标签: javascript html file download


【解决方案1】:

对于正在观看的人来说可以进行重大编辑

在您从https://github.com/eligrey/FileSaver.js/ 获得 FileSaver 并将其添加到您的页面后:

function getAndSaveUrl(url, name, mime) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'blob';

    xhr.onload = function(e) {
      if (this.status == 200) {
        var blob = new Blob([this.response], {type: mime});
        saveAs(blob, name); // Note: this is the FileSaver.js function
      }
    };
    xhr.send();
}

添加此功能

function doDownload(el) {
    getAndSaveUrl(el.href, el.textContent, "application/zip");
    return false;
}

将上面的 html 更改为

<div class="well text-left">
     <img src="https://api.kandy.io/v1.2/devices/content/thumbnail?key=UAT5b6f2e196b0a4e0793ff17ac44b6404c&amp;content_uuid=7DF8CD44-B614-4C4A-82F3-1C5E36B4CC02&amp;device_id=FF4A88BA0C2840749D5AB91584C29E4D&amp;thumbnail_size=500x500" style="width: 20px;"/>
     <a href="https://api.kandy.io/v1.2/devices/content?key=UAT5b6f2e196b0a4e0793ff17ac44b6404c&amp;content_uuid=7DF8CD44-B614-4C4A-82F3-1C5E36B4CC02&amp;device_id=FF4A88BA0C2840749D5AB91584C29E4D" onClick="return doDownload(this);">angular-treasure-overlay-spinner-master.zip</a>
</div>

在 Firefox 中测试,saveAs 之前的所有内容都可以正常工作 - 因为 jsfiddle 没有可用的 FileSaver.js,所以没有测试该部分 - 但我在工作中使用 fileSaver,并且没有任何问题

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2019-09-02
    • 2022-01-24
    相关资源
    最近更新 更多