[英]Type error:Cannot read property 'mv' of undefined
I want to upload all type of files(ex: csv,html files), im using the below code but it throwing a error called我想上传所有类型的文件(例如:csv、html 文件),我使用下面的代码,但它抛出一个名为
"Cannot read property 'mv' of undefined.."
“无法读取未定义的属性‘mv’..”
Here is my code,这是我的代码,
app.post('/file_upload', function (req, res) {
if(req.files){
let file = req.files.file;
file.mv('./Emailcsv/'+file,function(err,data){
if(err){
return res.send("error occured")
}
else{
console.log("saved");
res.send("File uploaded")
}
})
}
})
Here is my html code,这是我的html代码,
<div className="col-md-6" style={{marginTop:30}}>
<p>Hey There, Upload a HTML file here</p>
<form ref='uploadForm'
id='uploadForm'
action='/file_upload'
method='post'
encType="multipart/form-data">
<input type="file" name="sampleFile" />
<input type='submit' value='Upload!' />
</form>
</div>
You have to change the below line你必须改变下面的行
let file = req.files.file;
as,作为,
let file = req.files.sampleFile;
Hope this helps!希望这可以帮助!
Cannot read property 'mv' of undefined..
显示此错误是因为文件未定义, console.log(req.files)
并查看它包含的内容,然后如果您使用的是 multiform,也将值分配给文件,请查看express multer
req.files.file
is of type UploadedFile | req.files.file
的类型为UploadedFile | UploadedFile[] , so that means it could be a single file or an array. UploadedFile[] ,这意味着它可以是单个文件或数组。 In this case, if you are sure that you only receive a single file, you can replace the variable declaration with this line:
在这种情况下,如果您确定只收到一个文件,则可以用以下行替换变量声明:
let file = req.files.file as UploadedFile;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.