How to store image in node backend?

后端 未结 3 787
终归单人心
终归单人心 2021-01-16 06:08

I am using node and express for backend and Mongo DB for storage.I am using multer middleware for storing image,i got a problem and the problem is when i store an image from

相关标签:
3条回答
  • 2021-01-16 06:22

    It will become easy to store files after converting in string you just have to convert string in image in your frontend

    convert image in to base64 string using this code in your api and also don't forgot to delete file from upload folder

    "img": new Buffer.from(fs.readFileSync(req.file.path)).toString("base64")

    to delete the file

               let resultHandler = function (err) {
                    if (err) {
                        console.log("unlink failed", err);
                    } else {
                        console.log("file deleted");
                    }
                }
    
                fs.unlink(req.file.path, resultHandler);
    

    at your routes import multer

     `multer const multer = require('multer');
      const upload = multer({ dest: __dirname + '/uploads/images' });`
    

    Add upload.single('img') in your request

    router.post('/fellows-details', authorize([Role.ADMIN, Role.USER]), 
                upload.single('img'), usersController.fellowsdetails);
    
    0 讨论(0)
  • 2021-01-16 06:35

    The filesystem on Heroku is not suitable for the persistent storage of data. The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Each dyno boots with a clean copy of the filesystem from the most recent deploy You can't store files on Heroku. Heroku automatically removed your uploaded files. You have to use external services like Amazon S3 or Azure Blob Storage.

    https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-node-examples.html https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/blockblobclient?view=azure-node-latest

    If you don't want to set up an account with AWS to create an S3 bucket we also have add-ons here that handle storage and processing of static assets https://elements.heroku.com/addons

    0 讨论(0)
  • 2021-01-16 06:39

    var dateString = Date.now(); var storage = multer.diskStorage({

    destination: (req, file, cb) => {
    cb(null, './uploads/')
    
    
     },
    
    
    filename: (req, file, cb) => {
    cb(null,  dateString+'_'+file.originalname)
    
    
    }
    });
    
    
    
    var maxSize=3  1000  1000;
    
    
    var upload = multer({ storage: storage,
    limits: { fileSize: maxSize }})
    
    var upload = multer({ storage: storage , limits: { fileSize: maxSize }});
    app.post("/fileupload", upload.single("fileToUpload"), function (req, res) {
    
    
    console.log(req.file.originalname)
    var uploaded =  "http://localhost:4001/"+dateString+'_'+req.file.originalname;
    console.log(uploaded)
    res.json({status: 200,msg:'File saved successfully',data:uploaded});
    });
    
    0 讨论(0)
提交回复
热议问题