Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename file on download #5

Open
websirnik opened this issue Aug 25, 2014 · 9 comments
Open

Rename file on download #5

websirnik opened this issue Aug 25, 2014 · 9 comments

Comments

@websirnik
Copy link

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

@PixelsCommander
Copy link
Owner

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
Copy link

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
Copy link
Owner

Could you confirm this works in FireFox and Safari?

@nagasaidurga
Copy link

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

@Porco-Rosso
Copy link

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
Copy link

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
Copy link

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

@visonalhal
Copy link

@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
Copy link

download attribute will only work on the chrome browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants