如何设置从浏览器下载的文件名? [英] How to set name of file downloaded from browser?
问题描述
我正在编写一个Web应用程序,其中包括允许用户将文件上传到我的服务器。为了防止名称冲突和组织文件,我们将它们重命名在我的服务器上。通过跟踪原始文件名,我可以与文件的所有者进行通信,而无需知道我在后端更改了文件名。也就是说,直到他们去做文件下载。在这种情况下,系统会提示您下载一个不熟悉的名称的文件。
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.
我的问题是,是否有任何方式指定要下载的文件的名称只是HTML?所以用户上传一个名为abc.txt的文件,并将其重命名为xyz.txt,但是当它们下载时,我希望浏览器默认将文件保存为abc.txt。如果这是不可能的只是HTML,有什么办法吗?
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。我想你需要一个服务器端脚本,它将输出一个内容处理头。在PHP中,这样做是这样的:
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:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
如果您希望提供默认文件名,但不能自动下载,这似乎是有效的。 p>
if you wish to provide a default filename, but not automatic download, this seems to work.
header('Content-Disposition: filename="filetodownload.jpg"');
事实上,它是直接为您的文件提供服务的服务器,因此您无法进行交互使用HTML,因为HTML根本不涉及。
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.
这篇关于如何设置从浏览器下载的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!