【发布时间】:2021-05-06 01:15:42
【问题描述】:
我刚刚将我的第一个 nodejs 应用程序部署到包含文件上传选项的 heroku,当我检查 heroku 日志时,它在我的本地主机上运行良好,但在 heroku 中却没有,它显示错误:ENOENT:没有这样的文件或目录,重命名 '/tmp/ upload_b7273eba4e1532df2526c225f550de61' -> '/app/routes./uploads/data/1f86e8b13b3e0f95c8ef4d61f8d515ad.pdf'
我的代码是这样的
routes.post('/',(req,res) => {
var form=new formidable.IncomingForm();
form.parse(req,(err,fields,files) => {
//Check file type
console.log(files.resume.type);
if(files.resume.type !== 'image/jpeg' && files.resume.type !== 'application/pdf' && files.resume.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' && files.resume.type !== 'application/msword'){
res.render('upload',{msg:"Resume must be in jpg,doc,docx,pdf format"})
}else{
//If in resume format
crypto.randomBytes(16,(err,buf) => {
if(err) { return err}
const filename = buf.toString('hex') + path.extname(files.resume.name);
var old_path=files.resume.path;
console.log(old_path);
var new_path=path.join(__dirname+'./uploads/data/' + filename);
console.log(new_path);
fs.rename(old_path,new_path,(err) => {
if(err) throw err;
console.log("File Uploaded");
res.render('upload',{msg:"Upload success"})
})
});
}
})
});
我在 heroku 上的 old_path 是 /tmp/upload_b7273eba4e1532df2526c225f550de61 和 new_path 是 /app/routes./uploads/data/1f86e8b13b3e0f95c8ef4d61f8d515ad.pdf
【问题讨论】:
-
咳咳!这是你的名片吗:Change nodejs temporary directory
-
我应该在哪里使用 form.uploadDir='../uploads/data';在我的代码中??我对此一无所知,也没有找到任何关于此的教程
-
请注意 Heroku 不会持久化文件。如果这不是一个生产项目,这没什么大不了的,但是当 Heroku dyno 休眠时,任何文件都会被删除。
-
var form=new formidable.IncomingForm();之后的任何时间 一般认为/tmp在您无法控制的任何系统上都可能不是可靠的来源。而不是rename(),您可能应该将上传目录设置为您想要的位置。大多数用于分段上传的库都支持类似的概念。 -
这不是为了生产,这只是为了测试和我的学习,因为 godaddy 不允许在我的共享主机帐户中安装 node js,所以我在 heroku 上测试我的代码
标签: javascript node.js heroku