javascript 如何使用javascript更改下载时的文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7526849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:24:39  来源:igfitidea点击:

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

javascriptgreasemonkey

提问by supercoolville

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://website.com/video.mp4"

Example of what I want the filename to be saved as during download:
"The_title_renamed_with_javascript.mp4"

回答by Micah Henning

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.

这实际上可以通过 JavaScript 实现,尽管浏览器支持会参差不齐。您可以使用 XHR2 将文件作为 Blob 从服务器下载到浏览器,创建 Blob 的 URL,创建一个锚点,将其 href 属性设置为该 URL,将下载属性设置为您想要的任何文件名,然后点击链接。这适用于谷歌浏览器,但我还没有验证其他浏览器的支持。

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

回答by Fenton

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

您不能使用客户端 JavaScript 执行此操作,您需要设置响应标头...

.NET

。网

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

Or PHP

或 PHP

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

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

还提供您选择的其他服务器端语言。

回答by oezi

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

用于下载的文件名在标题中设置(查看“Content-Disposition”),它是在服务器端创建的。

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.

除非您有权访问服务器端,否则您无法在要链接的文件上使用纯 javascript 更改它(这样您可以传递一个附加参数来提供文件名并更改服务器端行为以设置标题匹配......但也可以使用纯html,不需要javascript)。结论:Javascript 对实现你想要的东西绝对没用。

回答by Brock Adams

You can probablydo this with a Chromeuserscript, but it cannot be done (yet) with Greasemonkey (Firefox) javascript.

可能可以使用 Chrome 用户脚本完成此操作,但(目前)无法使用 Greasemonkey (Firefox) javascript 完成此操作。

Workaround methods (easiest to hardest):

解决方法(最简单到最难):

  1. Add the links with Greasemonkey but use the excellent DownThemAll!add-onto 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()Docfunction 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.

  1. 添加与 Greasemonkey 的链接,但使用优秀的DownThemAll!用于下载和重命名视频的插件

  2. 按原样下载视频并使用批处理文件、shell 脚本、Python 程序等来重命名它们。

  3. 使用 Greasemonkey 的GM_xmlhttpRequest()Doc功能将文件发送到您控制的服务器上的您自己的 Web 应用程序。
    该服务器可以是您自己的运行XAMPP(或类似程序)的 PC 。

  4. 编写您自己的 Firefox 插件,而不是 Greasemonkey 脚本。附加组件具有所需的权限,而 Greasemonkey 没有。

回答by Saket

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

AFAIK,您将无法从客户端本身执行此操作。您可以首先使用所需名称将文件上传到服务器,然后将其提供给最终用户(在这种情况下将使用您的文件名)。

回答by Boris Mossounov

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

以防万一您正在为讨厌的下载 chrome 扩展程序寻找这样的解决方案,您应该查看 chrome.downloads API,它需要额外的权限(“下载”)并允许您指定文件名。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.

但是,我现在面临一个问题。我正在重构的 chrome 扩展有 600k+ 用户群,添加新权限将禁用所有这些扩展。所以这对我来说是不可行的解决方案,但如果你正在开发一个新的扩展,你绝对应该使用它。