我在node中使用multer.js来处理用户何时将视频上传到我的网站,并且最初工作正常,这就是我如此困惑的原因。现在上传的视频没有声音。
这是上传表格
<!DOCTYPE html>
<html>
<head>
<title>Video Upload</title>
</head>
<body>
<h1>Video Upload</h1>
<form action="http://localhost:80/upload"
enctype="multipart/form-data" method="POST">
<br>Video Title <input type="text" name="title" required/><br>
<br>
<input type="file" name="video" required/> <br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
这是相关的后端代码,对于 Multer,我还使用 FFmetadata 来保存视频的标题,并使用 uuid/v4 作为文件名。
var storage = multer.diskStorage({
destination: function(req, file, cb){
cb(null, __dirname + "/videos")
},
filename: function(req, file, cb){
cb(null, uuid() + ".mp4")
}
})
//max file size -- 500mb
const maxSize = 524288000;
var upload = multer({
storage: storage,
limits: {fileSize: maxSize},
fileFilter: function(req, file, cb){
var filetypes = /mp4/
var mimetype = filetypes.test(file.mimetype)
console.log(file)
var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if(mimetype && extname){
return cb(null, true)
}
//err
cb("Error: File upload only supports the " + "following filetypes - " + filetypes);
}
}).single("video")
app.get("/upload", function(req, res){
res.sendFile(__dirname + "/views/upload.html")
})
app.post("/upload", function(req, res){
upload(req, res, function(err){
console.log(req.body.title)
if(err){
res.send(err)
}
else{
metaData.write(req.file.path, {title: req.body.title}, function(err){
if(err){
res.send(err)
}
else{
res.send("uploaded")
}
})
}
})
})
我通过从req.file.path切换到req.file.filename来解决我的问题