简体   繁体   中英

How do I send image file with nedb

I using nedb .

How do I image upload?

I'm trying this code.

But, there is no saved evidence.

const Nedb = require('nedb');
const path = require('path');
const { remote } = require('electron');

const dbPath = remote.app.getPath('userData');
const users = new Nedb({
  autoload: true,
  filename: path.join(dbPath, 'users.db'),
});

// upload something image file.
document.querySelector('input[type=file]').addEventListener('change', (evt) => {
  const file = evt.target.files[0];

  users.insert(file, (err, docs) => {
    console.log(docs); // `{"_id": "hash value"}` only data
  });

});

I want to save for binary data.

Please tell me image upload with nedb.

As my solution is that you can't directly store but what you can do is to convert the image first into a string or base64 string using whatever library you can find but it will generate a very long string depending on the image type and quality.

I used the FileReader API provided by native JavaSCript :

 const fileReader = new FileReader();
    fileReader.addEventListener('load', e => {

                   let  imageStrings = (e.target.result);

                });
   fileReader.readAsDataURL(file);

So as you send this to Express or any web server you will find that it would cause a Size error message or Post 413 Payload Error Message in the terminal or console of your client so if you were using express then you could tune the JSON limit and the encoding parameters. Even if you save the image string it will be ugly to see in the nedb file (but hey it works at least now .)

Another option is to consider what was said on this thread on the GitHub page or https://github.com/louischatriot/nedb/issues/38 This link they talk about uploading the file into your file system and storing it elsewhere and make a database reference to it in your nedb database.

If you could find or anyone find a viable solution then please add a solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM