Multer Grid Fs Storage 下载而不是快速显示视频
Multer Grid Fs Storage downloading instead of displaying video in express
我正在关注 Travesy Media 关于 MongoDB gridfs 文件上传的教程。因此,当我尝试使用 readstream 在网络浏览器中显示文件时,它会下载视频而不是显示它!
这是我对该功能的代码:
router.get('/get_one/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists'
});
}
// Check if image
if (file.contentType === 'image/jpg' || file.contentType === 'video/mkv' || file.contentType === 'video/mp4' || file.contentType === "video/wmv" || file.contentType === "video/avi" || file.contentType === "video/flw") {
// Read output to browser
const readstream = gfs.createReadStream(file.filename);
readstream.pipe(res);
} else {
res.status(404).json({
err: 'Not an image'
});
}
请帮帮我!我不知道如何解决这个问题!
PS: file.contentType === "image/jpg"
是我试过的
记录 file.contentType 并查看是否得到“filetype/fileExtension”,例如 image/jpeg、video/mp4,如果没有得到这样的.只需在文件扩展名
上使用正则表达式进行设置
尝试在将流传输到响应之前在响应中设置 Content-type header;
const readstream = gfs.createReadStream(file.filename);
res.set('Content-Type',file.contentType)
readstream.pipe(res);
我正在关注 Travesy Media 关于 MongoDB gridfs 文件上传的教程。因此,当我尝试使用 readstream 在网络浏览器中显示文件时,它会下载视频而不是显示它! 这是我对该功能的代码:
router.get('/get_one/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists'
});
}
// Check if image
if (file.contentType === 'image/jpg' || file.contentType === 'video/mkv' || file.contentType === 'video/mp4' || file.contentType === "video/wmv" || file.contentType === "video/avi" || file.contentType === "video/flw") {
// Read output to browser
const readstream = gfs.createReadStream(file.filename);
readstream.pipe(res);
} else {
res.status(404).json({
err: 'Not an image'
});
}
请帮帮我!我不知道如何解决这个问题!
PS: file.contentType === "image/jpg"
是我试过的
记录 file.contentType 并查看是否得到“filetype/fileExtension”,例如 image/jpeg、video/mp4,如果没有得到这样的.只需在文件扩展名
上使用正则表达式进行设置尝试在将流传输到响应之前在响应中设置 Content-type header;
const readstream = gfs.createReadStream(file.filename);
res.set('Content-Type',file.contentType)
readstream.pipe(res);