PixelsCommander / Download-File-JS

Intelligent JavaScript solution for file downloading.
pixelscommander.com/polygon/downloadjs/
200 stars 110 forks source link

Rename file on download #5

Open websirnik opened 10 years ago

websirnik commented 10 years ago

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

PixelsCommander commented 10 years ago

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.

nagasaidurga commented 10 years ago

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

PixelsCommander commented 10 years ago

Could you confirm this works in FireFox and Safari?

nagasaidurga commented 10 years ago

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

Porco-Rosso commented 9 years ago

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.

DroidUnknown commented 7 years ago

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.

techhysahil commented 6 years ago

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

visonalhal commented 6 years ago

@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.

saurav-satpathy commented 1 year ago

download attribute will only work on the chrome browser.