Html 如何设置从浏览器下载的文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3102226/
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
How to set name of file downloaded from browser?
提问by Sparafusile
I'm writing a web application that, among other things, allows users to upload files to my server. In order to prevent name clashes and to organize the files, I rename them once they are put on my server. By keeping track of the original file name I can communicate with the file's owner without them ever knowing I changed the file name on the back end. That is, until they go do download the file. In that case they're prompted to download a file with a unfamiliar name.
我正在编写一个 Web 应用程序,除其他外,它允许用户将文件上传到我的服务器。为了防止名称冲突并组织文件,我将它们放在我的服务器上后重命名它们。通过跟踪原始文件名,我可以与文件所有者进行通信,而他们根本不知道我在后端更改了文件名。也就是说,直到他们去下载文件。在这种情况下,系统会提示他们下载名称不熟悉的文件。
My question is, is there any way to specify the name of a file to be downloaded using just HTML? So a user uploads a file named 'abc.txt' and I rename it to 'xyz.txt', but when they download it I want the browser to save the file as 'abc.txt' by default. If this isn't possible with just HTML, is there any way to do it?
我的问题是,有没有办法仅使用 HTML 指定要下载的文件的名称?所以用户上传了一个名为“abc.txt”的文件,我将它重命名为“xyz.txt”,但是当他们下载它时,我希望浏览器默认将文件保存为“abc.txt”。如果仅使用 HTML 无法做到这一点,有什么办法可以做到吗?
回答by Palantir
Can't find a way in HTML. I think you'll need a server-side script which will output a content-disposition header. In php this is done like this:
在 HTML 中找不到方法。我认为您需要一个服务器端脚本来输出内容处理标头。在 php 中,这是这样完成的:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
if you wish to provide a default filename, but not automatic download, this seems to work.
如果您希望提供默认文件名,而不是自动下载,这似乎可行。
header('Content-Disposition: filename="filetodownload.jpg"');
In fact, it is the server that is directly serving your files, so you have no way to interact with it from HTML, as HTML is not involved at all.
事实上,服务器直接为您的文件提供服务,因此您无法从 HTML 与其交互,因为根本不涉及 HTML。
回答by Mephiztopheles
When they click a button to download the file, you can add the HTML5 attribute download
where you can set the default filename.
当他们单击按钮下载文件时,您可以添加HTML5 属性download
,您可以在其中设置默认文件名。
That's what I did, when I created a xlsx file and the browser want to save it as zip file.
这就是我所做的,当我创建了一个 xlsx 文件并且浏览器想要将它保存为 zip 文件时。
<a href="path/to/file" download="renamed.txt">Download</a>
<a href="downloads/export.xlsx" download="Data-Export.xlsx">Download Export</a>
回答by xgqfrms
it's very easy by using HTML5 download
attribute!
使用 HTML5download
属性非常简单!
here is my codependemo.
这是我的codepen演示。
https://codepen.io/xgqfrms/full/GyEGzG/
https://codepen.io/xgqfrms/full/GyEGzG/
my screen shortcut.
我的屏幕快捷方式。
回答by cusspvz
Well, @Palantir's answer is, for me, the most correct way!
好吧,@Palantir 的答案对我来说是最正确的方法!
If you plan to use that with multiple files, then i suggest you to use (or make one) PHP Download Manager.
如果您打算将其与多个文件一起使用,那么我建议您使用(或制作一个)PHP 下载管理器。
BUT, if you want to make that to one or two files, I will suggest you the mod_rewrite option:
但是,如果您想将其制作成一两个文件,我会建议您使用 mod_rewrite 选项:
You have to create or edit your .htaccessfile on htdocsfolder and add this:
您必须在htdocs文件夹中创建或编辑您的.htaccess文件并添加以下内容:
RewriteEngine on
RewriteRule ^abc\.txt$ xyz.txt
With this code, users will download xyz.txtdata with the name abc.txt
使用此代码,用户将下载名为abc.txt 的xyz.txt数据
NOTE: Verify if you have already the "RewriteEngine on" on your file, if yes, add only the secondfor each file you wish to redirect.
注意:验证您的文件中是否已经有“ RewriteEngine on”,如果是,请为您希望重定向的每个文件添加第二个。
Good Luck;) (Sorry for my english)
祝你好运;) (对不起我的英语)