简体   繁体   English

如何使用HTML5重命名下载文件?

[英]How do i rename a file on download using HTML5?

i have been working on a website which will allow users to upload and download files. 我一直在一个允许用户上传和下载文件的网站上工作。 The file is renamed on upload and stored on my server. 该文件在上传时被重命名并存储在我的服务器上。 But when it is downloaded, it has to be renamed ie to its original name. 但是下载时,必须将其重命名,即为其原始名称。 Presently i am using the following to do so : 目前,我正在使用以下方法:

<a href="file link" download="Original name">Download</a>

But the "download" attribute does not work with firefox. 但是“下载”属性不适用于Firefox。 Any alternative using javascript or jquery ? 还有其他使用javascript或jquery的方法吗? I am using php for server side. 我在服务器端使用php。

Edit: 编辑:

Thanks for the solution using php. 感谢您使用php的解决方案。 But that is not what i am looking for. 但这不是我想要的。 I am using a custom file viewer using javascript. 我正在使用使用JavaScript的自定义文件查看器。 On clicking the thumbnail of the file, the viewer is displayed. 单击文件的缩略图后,将显示查看器。 An AJAX request is sent to get the link to the file. 发送AJAX请求以获取文件的链接。 The response (the link) is used to display the file, also is added to the tag for download. 响应(链接)用于显示文件,也被添加到标签中以进行下载。 A php solution would mean a page reload on every request, which i want to avoid. PHP解决方案将意味着在每个请求上重新加载页面,这是我想避免的。 So any javascript or jquery solutions ? 那么任何javascript或jquery解决方案?

在服务器上使用header("Content-Disposition: attachment; filename='Original name'")

Try Below Code 尝试以下代码

    header('Content-Disposition: attachment; filename='.basename('newfilename.txt'));
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize('filename.txt'));
    ob_clean();
    flush();
    readfile('filename.txt');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM