Javascript rename file on download

I want to be able to download a web file, but when the download dialog open, the filename is renamed.

Ex: File: http://<server>/<site>/test.txt

  • HTML Rename download link
  • Blob createObjectURL download not working in Firefox (but works when debugging)
  • Changing the value of the download attribute of an anchor with javascript
  • Running JavaScript downloaded with XMLHttpRequest
  • Force Download via Ajax and PHP
  • How do you handle download link that requires Authorization token using Angular?
  • and when I click to download the file, download dialog open with the file name: test001.txt.

    How can I achive that?

  • Prompting for download
  • Get page generated with Javascript in Python
  • Angular 2 download .CSV file click event with authentication
  • How to download a blob file from Chrome iOS in Javascript?
  • Running JavaScript downloaded with XMLHttpRequest
  • File download not working on firefox
  • 5 Solutions collect form web for “Javascript rename file on download”

    As InviS suggests, now there’s a download attribute on links.

    Example:

    <a href="http://server/site/test.txt" download="test001.txt">Download Your File</a>
    
    • spec
    • article
    • browser support (Chrome, FF, Opera, Android Browser >= 4.4.4 at the time of writing)

    This effect is accomplished by sending an additional header. You can use PHP, for example, to achieve this:

    URLs can be rewritten using .htaccess, (internally) redirecting the request to a PHP file. I will show a simple hard-coded example, of how the header can be set:

    <?php
        header('Content-type: text/plain');
        header('Content-Disposition: attachment; filename="test001.txt"');
        readfile('files/test.txt');
         //assuming that the files are stored in a directory, not in a database
    ?>
    

    Use download attribute of the link. But it works only in Chrome browser 🙂

    You can’t do that in Javascript. The “Save to”-dialog is openend by the browser and you can’t access that through JS, it’s a standard-dialog from the OS.

    Your server must provide the file with the desired name before the user clicks on the download-link.

    What’s the reason that you want to rename the downloaded file anyway?

    If what you want is to return a continuous type of file name, you have to write a script that will keep track of that and provide that file back to the user. One way is using plain PHP, or something more advanced if it is several files at a time, possibly a cURL call in php so it can generate several different files. I am guessing that is what you are looking on doing, but you can’t have the file name changed dynamically on the save box in that sense, you return the savename.txt filename.