gpt4 book ai didi

node.js - 无法重命名下载的文件

转载 作者:行者123 更新时间:2023-12-02 11:03:24 27 4
gpt4 key购买 nike

Express 版本是4.16.4

我下载了一个文件:

... <a href="' + url + '" title="T&eacute;l&eacute;charger la facture"><i class="fa fa-download"></i></a>

router.get("/downloadfacturemaintenance/:facture", function (req, res) {
var facture = req.params.facture;
res.download(path.join(__dirname, '../config/uploads/facture_maintenance/'+facture), "facture.png", function (err) {

console.log(err);

});
});

在运行时开始下载。但下载中的文件名是 res.download 第一个参数中的文件名;尽管我在第二个参数中定义了备用名称。但是第二个参数没有被考虑!那么为什么呢?

编辑:

这里是代码:

router.post("/maintenancereparation", function (req, res) {
async.parallel({
types_vehicule: function (cb_types_vehicule) {
connexion.query("select type_vehicule_id, type_vehicule_lib from " + utils.getPrefixNomTables() + "type_vehicule order by type_vehicule_id", function (err, rows) {
if (err)
throw err;
cb_types_vehicule(null, rows);
});
},
maintenances: function (cb_maintenance) {
connexion.query("select m.maintenance_id, m.maintenance_date_fin, v.immatriculation, m.maintenance_terminee, m.maintenance_facture from " + utils.getPrefixNomTables() + "maintenance m join " + utils.getPrefixNomTables() + "vehicule v on m.vehicule_id = v.vehicule_id order by m.maintenance_terminee, m.maintenance_date_fin", function (err, rows) {
if (err)
throw err;
cb_maintenance(null, rows);
});
}
}, function (err, results) {
res.render("maintenancereparation", { "types_vehicule": results.types_vehicule, "maintenances_new": results.maintenances });
});
});

<!DOCTYPE html>
<html lang="fr">
<head>
<style>
.center {
text-align: center;
}
</style>
</head>
<body>
<form id="frm" style="margin-top: 10px;">
<fieldset>
<legend>Maintenance / R&eacute;paration</legend>
<div class="row form-group col-sm-12">
<div class="radio">
<label><input type="radio" name="type_action" id="action_maintenance" /> Maintenance</label>
</div>
<div class="radio">
<label><input type="radio" name="type_action" id="action_reparation" /> Demande de r&eacute;paration</label>
</div>
</div>
<div id="div_maintenance">
<table class="table" id="list_maintenance">
<thead>
<tr>
<th>V&eacute;hicule</th>
<th>Date fin maintenance</th>
<th style="background: #FFDD00 !important; font-size: 13px !important;">Actions</th>
<th>Closed</th>
<th>facture</th>
</tr>
</thead>
<tbody>
<% maintenances_new.forEach(function(maintenance){ %>
<tr>
<td><%= maintenance.immatriculation %></td>
<td><%= maintenance.maintenance_date_fin %></td>
<td><%= maintenance.maintenance_id %></td>
<td><%= maintenance.maintenance_terminee %></td>
<td><%= maintenance.maintenance_facture %></td>
</tr>
<% }) %>
</tbody>
</table>
</div>
<div id="div_repair">
<div class="row form-group col-sm-12">
<label class="col-sm-1">Type v&eacute;hicule</label>
<select id="type_vehicule" name="type_vehicule">
<option value="">-- S&eacute;lectionner --</option>
<% types_vehicule.forEach(function(type_vehicule){ %>
<option value="<%= type_vehicule.type_vehicule_id %>"><%= type_vehicule.type_vehicule_lib %></option>
<% }) %>
</select>
</div>
</div>
</fieldset>
</form>
</body>

</html>
<script>
$(document).ready(function () {
$(":radio[name='type_action']").on("change", function () {
if ($("#action_maintenance").is(":checked")) {
$("#div_repair").hide();
$("#div_maintenance").show();
} else {
$("#div_maintenance").hide();
$("#div_repair").show();
}
});
$("#action_maintenance").prop("checked", true);
$(":radio[name='type_action']").change();
var list = $("#list_maintenance").DataTable({
"language": {
"loadingRecords": "Veuillez patienter...",
"processing": "Veuillez patienter...",
"zeroRecords": "Aucun résultat",
"emptyTable": "Aucun résultat"
},
"columns": [
{ "data": 0, className: "center" },
{ "data": 1, className: "center" }
],
"columnDefs": [
{
"targets": 1,
"render": function (data, type, row) {
return ( data == "" ? "" : (new Date(data)).toLocaleDateString() );
}
},
{
targets: [2],
orderable: false,
searchable: false,
render: function (data, type, row) {
if (row[3] == 1) {
var facture = row[4];
var fichier = facture.substring(facture.lastIndexOf("/") + 1);
var ext = fichier.substring(fichier.lastIndexOf("."));
//fichier = fichier.substring(0, fichier.indexOf("_"));
var url = "/track/vehicule/downloadfacturemaintenance/"+fichier;
return '<a href="' + url + '" data-facture="' + row[4] + '" title="T&eacute;l&eacute;charger la facture"><i class="fa fa-download"></i></a>';
}
else
return '<a href="javascript:void(0);" data-id="' + data + '" data-vehicule="' + row[0] + '" title="Cl&ocirc;turer"><i class="fa fa-flag"></i></a>';
}
},
{
targets: [3,4],
orderable: false,
searchable: false,
visible:false
}
]
});
list.on("click", "a[data-id]", function () {
var pk = $(this).data("id"), vehicule = $(this).data("vehicule");
$("#george_content").load("/track/vehicule/clotureMaintenance/" + pk + "/" + vehicule);
});
});
</script>

router.get("/downloadfacturemaintenance/:facture", function (req, res) {
var facture = req.params.facture;
var fichier = facture.substring(facture.lastIndexOf("/") + 1);
var ext = fichier.substring(fichier.lastIndexOf("."));
fichier = fichier.substring(0, fichier.indexOf("_"));
res.download(path.join(__dirname, '../config/uploads/facture_maintenance/'+facture), "facture.png", function (err) {

console.log(err);

});
});

最佳答案

这段代码对我有用

app.get('/download', function(req, res){
const file = `${__dirname}/demo.jpg`;
//res.attachment()
res.download(file,"DEMO.jpg"); // Set disposition and send it.
});

如果它不工作可能是由于缓存,请清除浏览器缓存,然后它应该可以工作。当我尝试复制该问题时,我也遇到了同样的问题,结果发现代码没有任何问题。然后我以隐身模式打开浏览器,它按预期工作。

在两种情况下检查响应 header Content-Disposition:

当您不将 filename 作为第二个参数传递给 res.download 时:

enter image description here

将文件名作为第二个参数传递:

enter image description here

测试

enter image description here

关于node.js - 无法重命名下载的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59987379/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com