我正在为一个小型办公室进行cms项目。所有者希望该应用程序为桌面应用程序。这不是问题,因为我们有电子。办公室为那里的客户做了很多包装工作,使他们很难将文件从一台计算机共享到另一台计算机。大多数文档都是PDF格式,但也有图像。 我将Vue.js用于ui,将mongoDB用于后端。
1st。我在渲染器进程中而不是在主要进程上连接到mongDB。 (我不知道这是否可以)。 2号我不确定是否需要gridFS。文件大小不是很大(不是一直都有)。 第三名我的模型使用猫鼬,猫鼬不支持gridFS。没什么大问题。
无论如何。我已成功以gridFS样式将文件上传到mongoDB。我查询数据块集合没有问题。但是流对我来说听起来像希腊文,我是希腊文。大戏。
如何正确读取readStreams?
这里是代码中的摘录,让您看看。任何帮助,我们将不胜感激。
// renderer.js
const mongoose = require('mongoose')
const { remote } = require('electron')
const { GridFSBucket } = require('mongodb')
const { dialog, BrowserWindow } = remote
let bucket = null
const connection = mongoose.connection
connection.on('connected', () => console.log('DB_Info: Connection establish. '));
connection.on('error', (error) => console.log('DB_Error: ', error))
connection.once('open', function onConnectionOpen() {
console.log('DB_Info: Database connection open.')
const db = connection.db
bucket = new GridFSBucket(db, { bucketName: 'files' })
// get the uploaded files from the gridFS files.collection
db.collection('files.files').find().toArray((err, docs) => {
if(err) return console.log('Files_Coll_Error: ', err)
if(!docs) return;
docs.forEach(doc => {
db.collection('files.chunks').findOne({files_id: doc._id})
.then(file => { console.log(file) })
// Here somehow I have to read the stream. But How???
// and the consept is to display them in the output div
//* (EDIT) older docs of mongDB 2.1 have code samples *//
let gotData = 0
let str = ''
bucket.openDownloadStream(doc._id)
.on('close', (data) => console.log('StreamClose: ', data))
.on('data', (buffer) => {
console.log('streamBuffer: ', buffer)
++gotData;
str += buffer.toString('utf8');
})
.on('end', () => {
console.log('streamEnd: ')
console.log(gotData)
console.log(str)
})
.on('error', (err) => console.log('streamError: ', err))
.on('file', (file) => console.log('streamFile', file))
// The onData event return the file buffer. Now what?
})
})
const db_uri = 'mongodb://localhost:27017/gridFS_test'
mongoose.connect(db_uri, { useNewUrlParser: true });
const currentWindow = BrowserWindow.getFocusedWindow()
const button = document.getElementById('ufa')
button.onclick = function uploadFileAction() {
// Open file dialog so user can select the files to upload.
dialog.showOpenDialog(currentWindow, {
title: 'Choose The Files To Upload',
properties: [ 'openFile', 'multiSelections' ]
}, (filePaths) => {
if(!filePaths)
return console.log('User didn\'t select any files. Return')
filePaths.forEach((filePath, index) => {
const fileName = path.basename(filePath)
const readStream = fs.createReadStream(filePath)
const uploadStream =
bucket.openUploadStream(`${ Date.now() }`, {})
.once('finish', () => { console.log('file uploaded..') })
.on('error', () => console.log('Upload_Stream_Error: ', error))
readStream.pipe(uploadStream)
})
})
}
<button id="ufa">upload File</button>
<div id="output"></div>