Link to rename HTML

I have an mp3 link like this:

http://example.com/932937293723.mp3

      

but I want to rename it when user uploads the file that way.

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

      

My code:

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

      

mp3 file stored on a remote server. And I am not the owner of this server. The HTML attribute download

seems like a pretty good solution. because it is not cross browser. Any cross-server solution to solve this problem? Perhaps Javascript: D

+3


source to share


3 answers


If you insist on working with an interface, try the following code. The getblob method is deprecated, but you need to update this side. Let me know.



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');

      

+1


source


You can use something like below (ASP.NET)

In ASPX

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

      

In ASP.NET



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

      

Look at other MIME types

Update # 1 - Using Javascript only, you can try something like this, although I have not tested in different browsers

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');

      

+1


source


At your end of the code, you can upload the file to your server, store it in a variable, rename it there, define the appropriate headers, and return it. this can happen as an ajax call initiated by a javascript click.

Please post more details on your support and I can help you.

+1


source







All Articles