【发布时间】:2020-02-17 03:52:08
【问题描述】:
当用户将视频上传到我的网站时,我在 node.js 中使用 multer 来处理,并且它最初运行良好,这就是我如此困惑的原因。现在上传的视频没有声音了。
这是上传表格
<!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 来保存视频的标题,并使用 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")
}
})
}
})
})
【问题讨论】:
-
我进一步测试了代码,发现它的元数据包给我带来了问题,所以现在它只给我音频而不是视频。
标签: javascript node.js video-streaming multer