node.js - nodejs how to store formdata data (file) as blob data of mongodb
过去多啦不再A梦
过去多啦不再A梦 2017-05-16 13:32:45
0
2
843

The front-end uploads a file file through formdata
The back-end framework is koa, and the co-busboy middleware is used to parse the request.
There is a simple example in the co-busboy document that can obtain a "file stream" through parsing "FileStream, and then write the file locally through the fs module of node. This is quite simple.

Now my problem is that I want to store the file in the form of blob instead of storing it locally.
Then I am thinking about how to transfer from FileStream.
This FileStream is printed as follows:

FileStream {
  _readableState:
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: [Object], tail: [Object], length: 1 },
     length: 10256,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: false,
     endEmitted: false,
     reading: true,
     sync: false,
     needReadable: true,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events: { end: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  truncated: false,
  _read: [Function],
  fieldname: 'homework',
  filename: '2017-1-18.json',
  encoding: '7bit',
  transferEncoding: '7bit',
  mime: 'application/octet-stream',
  mimeType: 'application/octet-stream' }

At that time, I saw an attribute called buffer, and I also tried the Buffer module in node, but it didn't seem to work.

In short, the question is how to convert the file stream to blob format?
Please give me some guidance

过去多啦不再A梦
过去多啦不再A梦

reply all(2)
伊谢尔伦

Decided not to store the files in the database

过去多啦不再A梦

MongoDB does not blob格式,你需要的是GridFS. Take a look at the driver documentation first, there are examples in it. If there is anything you don’t understand, discuss it later.
https://mongodb.github.io/nod...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!