I'm writing a web application that, among other things, allows users to upload files to my server. In order to prevent name *es 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无法做到这一点,有没有办法做到这一点?
5 个解决方案
#1
45
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根本不涉及。
#2
26
When they click a button to download the file, you can add the HTML5 attribute download
where you can set the default filename.
当他们单击按钮下载文件时,您可以添加HTML5属性下载,您可以在其中设置默认文件名。
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>
#3
1
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 .htaccess file on htdocs folder and add this:
您必须在htdocs文件夹上创建或编辑.htaccess文件并添加以下内容:
RewriteEngine on
RewriteRule ^abc\.txt$ xyz.txt
With this code, users will download xyz.txt data 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 second for each file you wish to redirect.
注意:验证您的文件上是否已经“RewriteEngine on”,如果是,则只为要重定向的每个文件添加第二个。
Good Luck ;) (Sorry for my english)
祝你好运;)(对不起我的英文)
#4
0
This will have to be done on the server side. You will have to have the old file name in a database somewhere, and then you could move it to a temp folder with the correct name and send that file to the user. TBQH i think you should consider avoiding name *es with sub directories and store the path to each file in your database instead of old name / new name.
这必须在服务器端完成。您必须在某个数据库中具有旧文件名,然后您可以将其移动到具有正确名称的临时文件夹并将该文件发送给用户。 TBQH我认为您应该考虑避免与子目录的名称冲突,并将路径存储到数据库中的每个文件而不是旧名称/新名称。
#5
0
it's very easy by using HTML5 download
attribute!
here is my codepen demo.
这是我的codepen演示。
https://codepen.io/xgqfrms/full/GyEGzG/
https://codepen.io/xgqfrms/full/GyEGzG/
my screen shortcut.
我的屏幕快捷方式
add
gif
demo.添加gif演示。
#1
45
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根本不涉及。
#2
26
When they click a button to download the file, you can add the HTML5 attribute download
where you can set the default filename.
当他们单击按钮下载文件时,您可以添加HTML5属性下载,您可以在其中设置默认文件名。
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>
#3
1
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 .htaccess file on htdocs folder and add this:
您必须在htdocs文件夹上创建或编辑.htaccess文件并添加以下内容:
RewriteEngine on
RewriteRule ^abc\.txt$ xyz.txt
With this code, users will download xyz.txt data 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 second for each file you wish to redirect.
注意:验证您的文件上是否已经“RewriteEngine on”,如果是,则只为要重定向的每个文件添加第二个。
Good Luck ;) (Sorry for my english)
祝你好运;)(对不起我的英文)
#4
0
This will have to be done on the server side. You will have to have the old file name in a database somewhere, and then you could move it to a temp folder with the correct name and send that file to the user. TBQH i think you should consider avoiding name *es with sub directories and store the path to each file in your database instead of old name / new name.
这必须在服务器端完成。您必须在某个数据库中具有旧文件名,然后您可以将其移动到具有正确名称的临时文件夹并将该文件发送给用户。 TBQH我认为您应该考虑避免与子目录的名称冲突,并将路径存储到数据库中的每个文件而不是旧名称/新名称。
#5
0
it's very easy by using HTML5 download
attribute!
here is my codepen demo.
这是我的codepen演示。
https://codepen.io/xgqfrms/full/GyEGzG/
https://codepen.io/xgqfrms/full/GyEGzG/
my screen shortcut.
我的屏幕快捷方式
add
gif
demo.添加gif演示。