[英]HTML5 download attribute doesn't work, Forcing file rename on download
我想要几个星期来更改正在下载的文件的名称。 但仍然没有运气。 而不是文件名是Chrysanthemum.jpg
,它成为文件的哈希,即241693260.jpg
在我的后端,我使用Node.js和Express.js来处理文件上传/下载。 我正在散列文件名和属性以确保文件不会被覆盖。
这是HTML代码:
<a class="btn btn-info" href="http://localhost:3000/getAttachments?name=Chrysanthemum.jpg&type=image%2Fjpeg&size=879394&lastModified=1247549551674&extension=jpg" download="Chrysanthemum.jpg" target="_blank">Download File</a>
这是我的后端:
app.use('/uploads', express.static(__dirname + "/uploads"));
app.get("/getAttachments", function (req, res) {
try {
var fileToBeSent = hashCode(req.query.name + req.query.type + req.query.size + req.query.lastModified + req.query.extension);
fileToBeSent += req.query.extension ? '.' + req.query.extension : '';
// res.sendFile("./uploads/" + fileToBeSent, {
// root: __dirname,
// "Content-Disposition": '"attachment; filename="' + (req.query.extension ? '.' + req.query.extension : '') + '"',
// "Content-Type": "application/octet-stream"
// });
res.redirect("/uploads/" + fileToBeSent);
} catch (err) {
console.log("an attempt to GET a file that doesn't exist.");
}
});
因此,当我在后端重命名文件名时 ,我正在尝试使用HTML5将文件重命名为它的原始名称,但我没有成功。
更新 :在同一问题中使用express的sendFile结果。
更新 :我的服务器使用跨源
我在一年前遇到了这个问题并且花了很多时间才发现这是Chrome Prove 35版及更高版本的潜在问题避免重复名称你可以使用静态id和增量。 希望这个答案会有所帮助
您所要做的就是提供要将文件下载的整个文件名。 在您的Content-Disposition中,您看起来并不像提供整个文件名。
res.sendFile(__dirname + "/uploads/" + fileToBeSent, {
headers: {
'Content-Disposition': 'attachment; filename="' + req.query.name + '"'
}
});
使用Node JS而不是HTML5重新命名文件是最好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.