我是Node.js的新手。下载文件时遇到问题。
在我的应用程序中,上传文件后,需要下载该文件。因此,我创建了一个href
链接,称为/downlaod
url。
下面是代码片段:
**index.ejs::**
<div class="uploadFile">
<h5>Upload Resume:</h5>
<form id="frmUploader" enctype="multipart/form-data" action="/Upload" method="post">
<input type="file" name="imgUploader" multiple />
<input type="submit" name="submit" id="btnSubmit" class="btn btn-default btn-lg page-scroll" value="Upload" />
</form>
<a href="/download" download><%=data.fileName %></a>
</div>
**App.js::**
var express=require("express");
var profileController=require("./controller/ProfileController");
var app=express();
app.set('view engine','ejs');
app.use(express.static('./public'));
profileController(app);
app.listen(5000);
ProfileController.js :::
var bodyParser = require('body-parser');
var multer=require("multer");
var path = require('path');
var download = require('download-file');
var scriptName="" ;
var url=("./uploadFiles/");
//console.log(scriptName);
var storage=multer.diskStorage({
destination: function(req,file,cb){
//console.log(req.cb);
cb(null,'./uploadFiles');
},
filename: function(req,file,cb){
scriptName=file.originalname;
console.log("inside"+scriptName);
cb(null, file.originalname);
}
})
var upload=multer({storage:storage});
var options = {
directory: "./uploadFiles/",
filename: scriptName
}
app.get("/download",function(req,res){
download(url, options, function(err){
if (err) throw err
console.log("download");
res.render('index');
})
});
执行程序后,出现以下错误:
[nodemon]观看:。 [nodemon]开始
node app.js
inside12BB.pdf ouside :: 12BB.pdf C:\ Users \ IBM_ADMIN \ Desktop \ personal \ node.js \ ProfileApp \ controller \ ProfileController.js:47 如果(错误)抛出错误 ^错误:getaddrinfo ENOTFOUND。 。:80 在GetAddrInfoReqWrap.onlookup处[完成时](dns.js:57:26)[nodemon]应用程序崩溃-等待文件更改后再开始...
我们非常感谢您的帮助。
经过研究,我终于解决了这个问题: 已编辑:
app.get("/download",function(req,res){
var data={fileName:scriptName};
console.log(data);
//url=url+scriptName;
// console.log(url);
res.download('./uploadFiles/'+scriptName);
});