【发布时间】:2017-10-05 08:43:37
【问题描述】:
我想在服务器端使用 mongodb 保存图像文件。 我想在正面显示这个(保存的)图像。
下面是我的服务器端代码。
const Video = new Schema({
...file owner, file path, etc,.
screenshot: {
type: Buffer
}
});
// Extract thumbnail by using ffmpeg.
let resDir = __dirname + '/resources/';
let videoFullPath = resDir + 'videos/' + newFileName;
let ssFullPath = resDir + 'screenshots/' + newFileName + '.png';
let execCommand = 'ffmpeg -i ' + videoFullPath +
' -f image2 -t 0.001 -ss 1.0 ' + ssFullPath;
childProcess.exec(execCommand);
// Save Video information DB.
let newVideo = new Video({
...file ownef, file path, etc,.
screenshot: ssFullPath,
});
newVideo.save((err, data) => {
if(err) throw err;
return res.json({success: true});
});
下面是我的前端代码。 (movieClipData 是 mongoDB 对象)
const imgSrc = 'data:image/png;base64,' + movieClipData.screenshot.data;
<img src={imgSrc}/>
我认为文件类型(base64,二进制...)会导致此问题。 所以我尝试将文件类型转换为base64,但没有成功。 (也许我做的不好……)
你能解释一下如何匹配文件类型吗?
【问题讨论】:
-
欢迎来到堆栈溢出。您能否提供比“但它不起作用”更详细的信息?您有错误消息吗?
-
@ruby_newbie,我的浏览器控制台上有 'net::ERR_INVALID_URL' 日志..
标签: javascript html mongodb reactjs mongoose