Javascript Javascript重命名下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7428831/
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
Javascript rename file on download
提问by bruno2007
I want to be able to download a web file, but when the download dialog open, the filename is renamed.
我希望能够下载 Web 文件,但是当下载对话框打开时,文件名被重命名。
Ex: File: http://<server>/<site>/test.txt
例如:文件: http://<server>/<site>/test.txt
and when I click to download the file, download dialog open with the file name: test001.txt
.
当我点击下载文件时,下载对话框打开,文件名:test001.txt
.
How can I achive that?
我怎样才能做到这一点?
回答by Dmitry Pashkevich
As InviS suggests, now there's a download
attribute on links.
正如 InviS 所建议的,现在download
链接上有一个属性。
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)
回答by Rob W
This effect is accomplished by sending an additional header. You can use PHP, for example, to achieve this:
这种效果是通过发送一个额外的标头来实现的。例如,您可以使用 PHP 来实现此目的:
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:
可以使用.htaccess
, (内部)将请求重定向到 PHP 文件来重写 URL 。我将展示一个简单的硬编码示例,说明如何设置标头:
<?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
?>
回答by Stephani Alves
You can used the download attribute on a link tag <a href="http://server/site/test.txt" download="test001.txt">Download Your File</a>
您可以在链接标签上使用下载属性 <a href="http://server/site/test.txt" download="test001.txt">Download Your File</a>
However, when content-disposition header is set on the server response, it will ignore the download attribute and the filename will be set to the filename in the content-disposition response header
但是,当在服务器响应上设置 content-disposition 标头时,它将忽略下载属性,并且文件名将设置为 content-disposition 响应标头中的文件名
You can accomplish this using axios, or any other fetch by doing this:
您可以使用 axios 或通过执行以下任何其他获取来完成此操作:
const downloadAs = (url, name) => {
Axios.get(url, {
headers: {
"Content-Type": "application/octet-stream"
},
responseType: "blob"
})
.then(response => {
const a = document.createElement("a");
const url = window.URL.createObjectURL(response.data);
a.href = url;
a.download = name;
a.click();
})
.catch(err => {
console.log("error", err);
});
};
usage:
用法:
downloadAs('filedownloadlink', 'newfilename');
Note: if your file is large, it will look like it is not doing anything until the whole file has been downloaded, so make sure you show some message or some indication to the user to let them know it is doing something
注意:如果您的文件很大,在整个文件下载完成之前它看起来不会做任何事情,因此请确保向用户显示一些消息或一些指示,让他们知道它正在做某事
回答by ValeriiVasin
Use download
attribute of the link. But it works only in Chrome browser :)
使用download
链接的属性。但它仅适用于 Chrome 浏览器 :)
回答by Truesky
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.
如果您想要返回连续类型的文件名,则必须编写一个脚本来跟踪该文件并将该文件返回给用户。一种方法是使用普通的 PHP,或者如果一次是多个文件,则使用更高级的方法,可能是 php 中的 cURL 调用,因此它可以生成多个不同的文件。我猜这就是您要执行的操作,但是从这个意义上说,您不能在保存框中动态更改文件名,您返回 savename.txt 文件名。
回答by Michael Sandino
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.
你不能在 Javascript 中做到这一点。“保存到”对话框由浏览器打开,您无法通过 JS 访问它,它是操作系统的标准对话框。
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?
您无论如何要重命名下载的文件的原因是什么?