簡體   English   中英

HTML重命名下載鏈接

[英]HTML Rename download link

我有這樣的mp3鏈接:

http://example.com/932937293723.mp3

但我想在用戶下載文件時重命名它

http://example.com/Artist - Title.mp3

我的代碼:

<a href="http://example.com/932937293723.mp3" download="Artist - Title.mp3">DOWNLOAD</a>

mp3文件存儲在遠程服務器中。 而且我不是該服務器的所有者。 HTML download屬性似乎不是很好的解決方案 因為它不是跨瀏覽器的。 任何跨瀏覽器解決方案來解決這個問題? 也許Javascript:D

您可以使用類似下面的內容(ASP.NET)

在ASPX中

<a href="FileDownloader.aspx?file=encoded_url_to_mp3">Download</a>

在ASP.NET中

Response.ContentType = "audio/mpeg3";
Response.AddHeader("content-disposition", "attachment;filename=New_file_name.mp3");
Server.Transfer(decoded_URL_of_MP3_file);

在這里查看其他MIME類型

更新#1 - 單獨使用Javascript,你可以試試這樣的東西,雖然我沒有在不同的瀏覽器中測試過

function Download(url, fancyFileName) 
    {
    var file = document.createElement('a');
    file.href = url;
    file.target = '_blank';
    file.download = fancyFileName;

    var event = document.createEvent('Event');
    event.initEvent('click', true, true);
    file.dispatchEvent(event);
    window.URL.revokeObjectURL(file.href);
    }

Download('http://server.com/file.mp3','Artist_file.mp3');

在后端代碼中,您可以將文件提取到服務器,將其存儲到變量,從那里重命名,定義相應的頭,然后返回。 這可能發生在javascript點擊啟動的ajax調用。

發布有關您支持的更多詳細信息,我可以為您提供更多幫助

如果您堅持從前端工作,請嘗試使用以下代碼。 折舊的getblob方法,但您需要更新該方法。 讓我知道。

function getBinary(file){
    var xhr = new XMLHttpRequest();  
    xhr.open("GET", file, false);  
    xhr.overrideMimeType("text/plain; charset=x-user-defined");  
    xhr.send(null);
    return xhr.responseText;
}
function sendBinary(data, url){
    var xhr = new XMLHttpRequest();
    xhr.open("POST", url, true);

    if (typeof XMLHttpRequest.prototype.sendAsBinary == "function") { // Firefox 3 & 4
        var tmp = '';
        for (var i = 0; i < data.length; i++) tmp += String.fromCharCode(data.charCodeAt(i) & 0xff);
        data = tmp;
    }
    else { // Chrome 9
        // http://javascript0.org/wiki/Portable_sendAsBinary
        XMLHttpRequest.prototype.sendAsBinary = function(text){
            var data = new ArrayBuffer(text.length);
            var ui8a = new Uint8Array(data, 0);
            for (var i = 0; i < text.length; i++) ui8a[i] = (text.charCodeAt(i) & 0xff);

            var bb = new BlobBuilder(); // doesn't exist in Firefox 4
            bb.append(data);
            var blob = bb.getBlob();
            this.send(blob);
        }
    }

    xhr.sendAsBinary(data); 
}

var data = getBinary("My music.mp3");
sendBinary(data,'http://www.tonycuffe.com/mp3/tailtoddle_lo.mp3');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM