How do I change a filename on-download with javascript?

后端 未结 6 1170
心在旅途
心在旅途 2021-02-02 16:03

The script adds a download link for videos (on a specific site). How do I change the filename to something else while downloading?

Example URL:
\"http://websit         


        
相关标签:
6条回答
  • 2021-02-02 16:32

    The filename for downloading is set in the header (take a look at "Content-Disposition"), wich is created on server-side.

    There's no way you could change that with pure javascript on a file you're linking to unless you have access to the server-side (that way you could pass an additional parameter giving the filename and change the server-side behaviour to set the header to match that... but that would also be possible with pure html, no need for javascript). Conclusion: Javascript is absolute useless to achive what you want.

    0 讨论(0)
  • 2021-02-02 16:38

    You can't do this with client-side JavaScript, you need to set the response header...

    .NET

    Response.AddHeader("Content-Disposition", "inline;filename=myname.txt")
    

    Or PHP

    header('Content-Disposition: inline;filename=myname.txt')
    

    Also available in other server-side languages of your choice.

    0 讨论(0)
  • 2021-02-02 16:38

    Just in case you are looking for such a solution for your nasty downloading chrome extension, you should look into chrome.downloads API, it needs additional permission ('downloads') and allows you to specify filename. https://developer.chrome.com/extensions/downloads

    However there is a problem I'm facing right now. The chrome extension I'm refactoring has 600k+ user base and adding a new permission would disable the extension for all of them. So it is no-go solution for me, but if you are developing a new extension you definitely should use it.

    0 讨论(0)
  • 2021-02-02 16:42

    AFAIK, you will not be able to do this right from the client itself. You could first upload the file onto the server with the desired name, and then serve it back up to the end user (in which case your file name would be used).

    0 讨论(0)
  • 2021-02-02 16:55

    This actually is possible with JavaScript, though browser support would be spotty. You can use XHR2 to download the file from the server to the browser as a Blob, create a URL to the Blob, create an anchor with its href property set to that URL, set the download property to whatever you want the filename to be, and then click the link. This works in Google Chrome, but I haven't verified support in other browsers.

    window.URL = window.URL || window.webkitURL;
    
    var xhr = new XMLHttpRequest(),
          a = document.createElement('a'), file;
    
    xhr.open('GET', 'someFile', true);
    xhr.responseType = 'blob';
    xhr.onload = function () {
        file = new Blob([xhr.response], { type : 'application/octet-stream' });
        a.href = window.URL.createObjectURL(file);
        a.download = 'someName.gif';  // Set to whatever file name you want
        // Now just click the link you created
        // Note that you may have to append the a element to the body somewhere
        // for this to work in Firefox
        a.click();
    };
    xhr.send();
    
    0 讨论(0)
  • 2021-02-02 16:55

    You can probably do this with a Chrome userscript, but it cannot be done (yet) with Greasemonkey (Firefox) javascript.

    Workaround methods (easiest to hardest):

    1. Add the links with Greasemonkey but use the excellent DownThemAll! add-on to download and rename the videos.

    2. Download the videos as-is and use a batch file, shell-script, Python program, etc. to rename them.

    3. Use Greasemonkey's GM_xmlhttpRequest()Doc function to send the files to your own web application on a server you control.
      This server could be your own PC running XAMPP (or similar).

    4. Write your own Firefox add-on, instead of a Greasemonkey script. Add-ons have the required privileges, Greasemonkey does not.

    0 讨论(0)
提交回复
热议问题