0

i want to upload the file from the html page and store it in mongodb data base using node.js and using multer npm package as middleware between client and server

Below is my app.js file:

var express = require('express')
, multer = require('multer');

var app = express();
var multer  =   require('multer');

var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now());
  }
});
var upload = multer({ storage : storage}).single('userPhoto');

app.post('/api/photo',function(req,res){
upload(req,res,function(err) {
    if(err) {
        return res.end("Error uploading file.");
    }
    res.end("File is uploaded");
});
});

And HTML form is:

<form id="uploadForm" enctype="multipart/form-data" action="/api/photo"    method="post">
  <div class="azureD" style="display:none;">
    <div class="pull-left">
      <label class="labelTemp">Subscription ID</label>
      <div class="clickRole addStoTabWid">
      <input type="text" id="" placeholder="" style="border:none;width:100%;">  
    </div>
  </div>
  <div class="pull-left">
    <label class="labelTemp">Upload .pem file</label>
    <div class="clickRole addStoTabWid">
      <input type="file" name="userPhoto" id="" placeholder=""  style="border:none;width:100%;">
    </div>
  </div>
  <div class="modal-footer">
    </br><!--<a class="cancelPoup">Cancel</a>&nbsp;
    <button class="redButton">Create</button>-->
    <input type="submit" value="Upload Image" name="submit">
  </div>
</form>

and i want to upload the user selected file into mongodb or local disk please help me out of this problem?

It is giving as file is uploaded as result but its not uploadign into folder and in node.js console i am getting ".write(string, encoding, offset, length) is deprecated. Use write(string[, offset[, length]][, encoding]) instead. multer"

1 Answer 1

2

you can always use mongoose to save the file information. prepare the schema calls for the schema and perform this

 //call for the mongoose schema
 //example :var emp = require('../models/employees.js');
 app.post('/api/photo', function(req, res) {
     upload(req, res, function(err) {
         if (err) {
             return res.end("Error uploading file.");
         }
         var upl = new emp({
             picture: req.file.originalname
         }); //creating object the mongoose schema
         upl.save(function(err, docs) {
             if (err) {
                 console.log(err);
             }
             res.json(docs);
         });
         res.end("File is uploaded");
     });
 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.