2017-05-07 71 views
1

我想在服务器端使用mongodb保存图像文件。 ,我想在正面显示这个(保存的)图像。如何检索图像与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,但它没有工作。 (也许我不会这样做......)

你能解释一下如何匹配文件类型吗?

+0

欢迎使用堆栈溢出。你能否给出比“但它不工作”更详细的信息?你有错误信息吗? –

+0

@ruby_newbie,我的浏览器控制台上有'net :: ERR_INVALID_URL'日志.. –

回答

0

我不确定您的movieClipData.screenshot对象的data属性与您描述的后端对象相关。

你有没有尝试直接使用它作为一个URL,因为你有screenshot: ssFullPath,该screenshot不如在Schema描述的Buffer,但通过的ffmpeg保存文件路径的字符串。

<img src={movieClipData.screenshot} /> 
+0

我试过src = {movieClipData.screenshot}和“”这个日志打印出来。 –

+0

和我检查movieClipData.screenshot对象。 “对象{type:”Buffer“,data:Array(225988)}”“。 –

+0

看起来我们错过了后端处理的几个步骤。你可以添加更多的细节吗? – YoannM