Skip to main content

how How can I save my image data from frontendfront end to mongooseMongoose database using nodeNode.js server

I have backenda back end using node.js and a frontendfront end using jquery, javascript, ajax, and bootstrap. I want to upload an Imageimage from frontendfront end and save it to the mongoose database.Here Here the file is loading successfully and image will be in the location called /public/img but. But how can I save those image information into the database using apian API call.My code
Below is in belowmy code:

/server.js/server.js

/api/requests.js/api/requests.js

/schema/Data.js/schema/Data.js

'use strict'; exports = module.exports = function(app, mongoose) {   var dataSchema = new mongoose.Schema({   image_title : { type: String, unique: true, lowercase: true },   image_url : { type: String, unique: true, lowercase: true }   });   app.db.model('Data', dataSchema);  }; 

/models.js/models.js

/routes.js/routes.js

{  image_title: exampleImage,  image_url: xyzscjnscncsl.exampleImage.png } 

/public/index.html/public/index.html

/public/javascript/image.js/public/javascript/image.js

/* How to save image data from here to the mongoose db using api called */an API call?

$(document).ready(function() {   $('#uploadForm').submit(function() {   $("#status").empty().text("File is uploading...");    $(this).ajaxSubmit({     error: function(xhr) {   status('Error: ' + xhr.status);   },     success: function(response) {   console.log(response)   $("#status").empty().text(response);   }   });   return false;   });  }); 

how can I save my image data from frontend to mongoose database using node.js server

I have backend node.js and a frontend using jquery, javascript, ajax, bootstrap. I want to upload an Image from frontend and save it to the mongoose database.Here file is loading successfully and image will be in the location called /public/img but how can I save those image information into the database using api call.My code is in below:

/server.js

/api/requests.js

/schema/Data.js

'use strict'; exports = module.exports = function(app, mongoose) { var dataSchema = new mongoose.Schema({ image_title : { type: String, unique: true, lowercase: true }, image_url : { type: String, unique: true, lowercase: true } }); app.db.model('Data', dataSchema);  }; 

/models.js

/routes.js

{ image_title: exampleImage, image_url: xyzscjnscncsl.exampleImage.png } 

/public/index.html

/public/javascript/image.js

/* How to save image data from here to mongoose using api called */

$(document).ready(function() {   $('#uploadForm').submit(function() {   $("#status").empty().text("File is uploading...");    $(this).ajaxSubmit({     error: function(xhr) {   status('Error: ' + xhr.status);   },     success: function(response) {   console.log(response)   $("#status").empty().text(response);   }   });   return false;   });  }); 

How can I save my image data from front end to Mongoose database using Node.js server

I have a back end using node.js and a front end using jquery, javascript, ajax and bootstrap. I want to upload an image from front end and save it to the mongoose database. Here the file is loading successfully and image will be in the location called /public/img. But how can I save those image information into the database using an API call.
Below is my code:

/server.js

/api/requests.js

/schema/Data.js

'use strict'; exports = module.exports = function(app, mongoose) {   var dataSchema = new mongoose.Schema({   image_title : { type: String, unique: true, lowercase: true },   image_url : { type: String, unique: true, lowercase: true }   });   app.db.model('Data', dataSchema); }; 

/models.js

/routes.js

{  image_title: exampleImage,  image_url: xyzscjnscncsl.exampleImage.png } 

/public/index.html

/public/javascript/image.js

How to save image data from here to the mongoose db using an API call?

$(document).ready(function() { $('#uploadForm').submit(function() { $("#status").empty().text("File is uploading..."); $(this).ajaxSubmit({ error: function(xhr) { status('Error: ' + xhr.status); }, success: function(response) { console.log(response) $("#status").empty().text(response); } }); return false; }); }); 
added 5 characters in body; edited title
Source Link
ronny
  • 101
  • 1
  • 5
  • 11

how can I save my image data from frontend to mongoose database using node.js server. image save api is **/api/photoGallery**

I have backend node.js and a frontend using jquery, javascript, ajax, bootstrap. I want to upload an Image from frontend and save it to the mongoose database.Here file is loading successfully and image will be in the location called /public/img but how can I save those image information into the database using api called /api/photoGallerycall.myMy code is in below:

 'use strict'; module.exports = function(app, router, passport) {   var requests = require('./api/requests').init(passport);   router.get('/', requests.root);     router.post('/api/photoGallery'addPhoto/v1', requests.addData);//I want to save my image   data through these apirouter.get('/api/getPhoto/v1', requests.getPhoto); }; 

how can I save my image data from frontend to mongoose database using node.js server. image save api is **/api/photoGallery**

I have backend node.js and a frontend using jquery, javascript, ajax, bootstrap. I want to upload an Image from frontend and save it to the mongoose database.Here file is loading successfully and image will be in the location called /public/img but how can I save those image information into the database using api called /api/photoGallery.my code is in below:

'use strict'; module.exports = function(app, router, passport) { var requests = require('./api/requests').init(passport); router.get('/', requests.root); router.post('/api/photoGallery', requests.addData);//I want to save my image data through these api. }; 

how can I save my image data from frontend to mongoose database using node.js server

I have backend node.js and a frontend using jquery, javascript, ajax, bootstrap. I want to upload an Image from frontend and save it to the mongoose database.Here file is loading successfully and image will be in the location called /public/img but how can I save those image information into the database using api call.My code is in below:

 'use strict'; module.exports = function(app, router, passport) {   var requests = require('./api/requests').init(passport);   router.get('/', requests.root);     router.post('/api/addPhoto/v1', requests.addData);   router.get('/api/getPhoto/v1', requests.getPhoto); }; 
added 406 characters in body
Source Link
ronny
  • 101
  • 1
  • 5
  • 11
var express = require("express"); var multer = require('multer'); var path = require('path'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var app = express(); app.appname="photogallery"; //config mongoose app.db = mongoose.createConnection('localhost/'+app.appname); app.db.on('error', console.error.bind(console, 'mongoose connection error: ')); app.db.once('open', function () { //Storage is all good }); //Routes and acl var router = express.Router(); require('./routes')(app, router, passport); var file_url = '';  var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, 'public/img/'); }, filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now());   file_url = file.fieldname + '-' + Date.now() + '.' + file.originalname.split('.')[file.originalname.split('.').length -1] callback(null, file_url); } }); var upload = multer({ storage : storage}).single('userPhoto'); app.use(express.static(path.join(__dirname, '/public/'))); 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"); res.json({error_code:0,err_desc:null,file_url:'img/'+file_url}); /*Now I want to save this file_url to image_url using api /api/photoGallery, please help me to save these information to the db */  }); }); //config express app.set('secret','thisshouldnotbeinplaintext'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(passport.initialize()); app.use(router); app.listen(5050,function(){ console.log("Working on port 5050"); }); 
var express = require("express"); var multer = require('multer'); var path = require('path'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var app = express(); app.appname="photogallery"; //config mongoose app.db = mongoose.createConnection('localhost/'+app.appname); app.db.on('error', console.error.bind(console, 'mongoose connection error: ')); app.db.once('open', function () { //Storage is all good }); //Routes and acl var router = express.Router(); require('./routes')(app, router, passport); var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, 'public/img/'); }, filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now()); } }); var upload = multer({ storage : storage}).single('userPhoto'); app.use(express.static(path.join(__dirname, '/public/'))); 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"); }); }); //config express app.set('secret','thisshouldnotbeinplaintext'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(passport.initialize()); app.use(router); app.listen(5050,function(){ console.log("Working on port 5050"); }); 
var express = require("express"); var multer = require('multer'); var path = require('path'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var app = express(); app.appname="photogallery"; //config mongoose app.db = mongoose.createConnection('localhost/'+app.appname); app.db.on('error', console.error.bind(console, 'mongoose connection error: ')); app.db.once('open', function () { //Storage is all good }); //Routes and acl var router = express.Router(); require('./routes')(app, router, passport); var file_url = '';  var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, 'public/img/'); }, filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now());   file_url = file.fieldname + '-' + Date.now() + '.' + file.originalname.split('.')[file.originalname.split('.').length -1] callback(null, file_url); } }); var upload = multer({ storage : storage}).single('userPhoto'); app.use(express.static(path.join(__dirname, '/public/'))); 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"); res.json({error_code:0,err_desc:null,file_url:'img/'+file_url}); /*Now I want to save this file_url to image_url using api /api/photoGallery, please help me to save these information to the db */  }); }); //config express app.set('secret','thisshouldnotbeinplaintext'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(passport.initialize()); app.use(router); app.listen(5050,function(){ console.log("Working on port 5050"); }); 
added 128 characters in body; edited title
Source Link
ronny
  • 101
  • 1
  • 5
  • 11
Loading
added 478 characters in body; edited title
Source Link
ronny
  • 101
  • 1
  • 5
  • 11
Loading
Source Link
ronny
  • 101
  • 1
  • 5
  • 11
Loading