简体   繁体   English

如何使用Anchor标签在Firefox中下载mp3文件?

[英]How to download a mp3 file in Firefox using Anchor tag?

In my website I'm trying to download a file using following code. 在我的网站上,我尝试使用以下代码下载文件。 It works normally in chrome and downloads the file. 它在chrome中正常工作并下载文件。 But in firefox its opening file in other tab and playing it. 但是在firefox中,其打开文件在其他选项卡中并播放。 Please suggest any other method to download mp3 请建议其他方法下载mp3

FYI: Files are getting downloaded from other websites and please give me an answer related to mp3 file download not any text downloads. 仅供参考:文件正在从其他网站下载,请给我一个与mp3文件下载相关的答案,而不是任何文本下载。 May be it is Related to cross origin downloads. 可能与跨源下载有关。

Code: 码:

    var anchor = document.createElement('a');
    anchor.href= 'http://www.example.com/Albums/NameOfalbum/songname.mp3';  //Cross Origin
    anchor.download= 'FileName.mp3';
    anchor.target='_blank';
    anchor.id='download';
    if(navigator.userAgent.indexOf("Chrome") != -1 )
         anchor.click();
    else if(navigator.userAgent.indexOf("Firefox") != -1 )
         $("#download")[0].click();

You can use hidden iframe like. 您可以使用隐藏的iframe之类的。

function downloadURL(url) {
    var hiddenIFrameID = 'hiddenDownloader',
        iframe = document.getElementById(hiddenIFrameID);
    if (iframe === null) {
        iframe = document.createElement('iframe');
        iframe.id = hiddenIFrameID;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
    }
    iframe.src = url;
};

Here is HTML code: 这是HTML代码:

<a href="#" onclick="downloadURL('path/of/mp3/file')">Download File</a>

Or if you want to use server side language like PHP then please follow this link. 或者,如果您想使用服务器端语言(如PHP),请点击此链接。

http://www.web-development-blog.com/archives/php-download-file-script/ http://www.web-development-blog.com/archives/php-download-file-script/

try this code: 试试这个代码:

$('#downloadButton').click(function () {
    // some data to export
    var data = [{
        "title": "Book title 1",
        "author": "Name1 Surname1"
    }, {
        "title": "Book title 2",
        "author": "Name2 Surname2"
    }, {
        "title": "Book title 3",
        "author": "Name3 Surname3"
    }, {
        "title": "Book title 4",
        "author": "Name4 Surname4"
    }];

    // prepare CSV data
    var csvData = new Array();
    csvData.push('"Book title","Author"');
    data.forEach(function (item, index, array) {
        csvData.push('"' + item.title + '","' + item.author + '"');
    });

    // download stuff
    var fileName = "data.csv";
    var buffer = csvData.join("\n");
    var blob = new Blob([buffer], {
        "type": "text/csv;charset=utf8;"
    });
    var link = document.createElement("a");

    if (link.download !== undefined) { // feature detection
        // Browsers that support HTML5 download attribute
        link.setAttribute("href", window.URL.createObjectURL(blob));
        link.setAttribute("download", fileName);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    } else {
        alert('CSV export only works in Chrome, Firefox, and Opera.');
    }
});

html: HTML:

<div class="toggle-button" id="downloadButton"><span>Export to CSV</span>
</div>

Download attribute not working in Firefox 下载属性在Firefox中不起作用

PS: HTML anchor tag download attribute not working in Firefox for jpg and png files PS: HTML锚标记下载属性在Firefox中不适用于jpg和png文件

var links = document.querySelectorAll("a"), i = 0, lnk;

while(lnk = links[i++]) {
  if (lnk.dataset.link.length) lnk.onclick = toBlob;
}

function toBlob(e) {
  e.preventDefault();
  var lnk = this, xhr = new XMLHttpRequest();
  xhr.open("GET", lnk.dataset.link);
  xhr.responseType = "blob";
  xhr.overrideMimeType("octet/stream");
  xhr.onload = function() {
    if (xhr.status === 200) {
      window.location = (URL || webkitURL).createObjectURL(xhr.response);
    }
  };
  xhr.send();
}

html: HTML:

<a href="#" data-link="image.jpg">Click to download</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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