Download-File-JS icon indicating copy to clipboard operation
Download-File-JS copied to clipboard

Rename file on download

Open websirnik opened this issue 10 years ago • 9 comments

Is it possible to rename a file on download? It is doable with download attribute - http://stackoverflow.com/a/15970037/257815

websirnik avatar Aug 25 '14 22:08 websirnik

Yes, but as I know download attribute is not completely cross-browser way to start download. Not sure is this feature possible to implement at all.

PixelsCommander avatar Aug 31 '14 00:08 PixelsCommander

yes you can rename the file name.. Rewrite the download.js file as below window.downloadFile = function (sUrl,name) { //console.log("name"+name); //iOS devices do not support downloading. We have to inform user about this. if (/(iP)/g.test(navigator.userAgent)) { alert('Your device does not support files downloading. Please try again in desktop browser.'); return false; }

//If in Chrome or Safari - download via virtual link click
if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
    //Creating new link node.
    var link = document.createElement('a');
    link.href = sUrl;

    if (link.download !== undefined) {
        //Set HTML5 download attribute. This will prevent file from opening if supported.
        console.log("setting file name");
      //  var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
        link.download = name;
    }

    //Dispatching click event.
    if (document.createEvent) {
        var e = document.createEvent('MouseEvents');
        e.initEvent('click', true, true);
        link.dispatchEvent(e);
        return true;
    }
}else{//other than chrome and safari
     var link = document.createElement('a');
     link.href = sUrl;

     if (link.download !== undefined) {
         //Set HTML5 download attribute. This will prevent file from opening if supported.
        console.log("setting file name");
         //var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
         link.download = name+".pdf";(you need to mention the .extension of the file)
     }

     //Dispatching click event.
     if (document.createEvent) {
         var e = document.createEvent('MouseEvents');
         e.initEvent('click', true, true);
         link.dispatchEvent(e);
         return true;
     }
}

// Force file download (whether supported by server).
if (sUrl.indexOf('?') === -1) {
    sUrl += '?download';
}

window.open(sUrl, '_self');
return true;

}

window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;

Call the download function as below download('./filepath','filename');

nagasaidurga avatar Sep 18 '14 06:09 nagasaidurga

Could you confirm this works in FireFox and Safari?

PixelsCommander avatar Sep 18 '14 09:09 PixelsCommander

yeah.I worked with fire fox and chrome . but i have to check whether it is working in safari or not

nagasaidurga avatar Sep 18 '14 09:09 nagasaidurga

Hi i've implemented this myself and it doesn't seem to work. Even on chrome that supports the download attribute over the response header, it doesn't seem to work.

Porco-Rosso avatar May 03 '15 22:05 Porco-Rosso

I have tried it as well, it is not working on chrome. I have and AWS s3 file with name say XYZ.ext and I want it to be ABC.ext but this is not working. Even if i create a normal download attribute in anchor tag it doesn't work.

DroidUnknown avatar May 10 '17 12:05 DroidUnknown

Download attribute also doesn't work while getting assets from other domain.

techhysahil avatar Jan 18 '18 16:01 techhysahil

@DroidUnknown You can use a nginx to get s3 file url by proxy, like this

location ~ ^\/(s3file){
	   proxy_passhttps://s3xxx.amazonaws.com/		;
           proxy_set_header    X-Real-IP   $remote_addr;
           client_max_body_size    100m;
        }

then set download attribute in anchor tag to change file name.

visonalhal avatar Mar 16 '18 07:03 visonalhal

download attribute will only work on the chrome browser.

saurav-satpathy avatar Feb 14 '23 09:02 saurav-satpathy