Spa*_*ile 50 html download savefiledialog
我正在编写一个Web应用程序,除其他外,它允许用户将文件上传到我的服务器.为了防止名称冲突和组织文件,我将它们放在我的服务器上后重命名.通过跟踪原始文件名,我可以与文件的所有者进行通信,而他们不知道我在后端更改了文件名.也就是说,直到他们去下载文件.在这种情况下,系统会提示他们下载一个名称不熟悉的文件.
我的问题是,有没有办法只使用HTML指定要下载的文件的名称?因此,用户上传名为'abc.txt'的文件并将其重命名为'xyz.txt',但是当他们下载它时,我希望浏览器默认将文件保存为'abc.txt'.如果仅使用HTML无法做到这一点,有没有办法做到这一点?
Pal*_*tir 60
无法在HTML中找到方法.我想你需要一个服务器端脚本来输出内容处理头.在PHP中,这样做:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
Run Code Online (Sandbox Code Playgroud)
如果你想提供一个默认的文件名,但不是自动下载,这似乎有效.
header('Content-Disposition: filename="filetodownload.jpg"');
Run Code Online (Sandbox Code Playgroud)
实际上,它是直接为您的文件提供服务的服务器,因此您无法通过HTML与其进行交互,因为HTML根本不涉及.
Mep*_*les 47
当他们单击按钮下载文件时,您可以添加HTML5属性download
,您可以在其中设置默认文件名.
这就是我做的,当我创建一个xlsx文件并且浏览器想要将其保存为zip文件时.
<a href="path/to/file" download="renamed.txt">Download</a>
<a href="downloads/export.xlsx" download="Data-Export.xlsx">Download Export</a>
Run Code Online (Sandbox Code Playgroud)
a
标签download
属性codepen
现场演示https://codepen.io/xgqfrms/full/GyEGzG/
我的屏幕快捷方式。
文件是否可下载取决于服务器的响应配置,例如Content-Type
, Content-Disposition
;
下载文件extensions
是可选的,也取决于服务器的配置。
'Content-Type': 'application/octet-stream',
// it means unknown binary file,
// browsers usually don't execute it, or even ask if it should be executed.
'Content-Disposition': `attachment; filename=server_filename.filetype`,
// if the header specifies a filename,
// it takes priority over a filename specified in the download attribute.
Run Code Online (Sandbox Code Playgroud)
blob
网址文件'Content-Type': 'application/octet-stream',
// it means unknown binary file,
// browsers usually don't execute it, or even ask if it should be executed.
'Content-Disposition': `attachment; filename=server_filename.filetype`,
// if the header specifies a filename,
// it takes priority over a filename specified in the download attribute.
Run Code Online (Sandbox Code Playgroud)
https://cdn.xgqfrms.xyz/HTML5/Blob/index.html
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#download
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition