node.js - How to upload file using nodejs and express -


i lost on how upload file using nodejs. first time doing it. have followed many 'guides' , none have worked. try format code fit doing , fails. hoping can guide me in right direction or help.

my code have below.

startup.js model file  var mongoose = require("mongoose");  var startupschema = new mongoose.schema({     about_startup: {         startup_name: string,         startup_url: string,         short_description: string,         long_description: string,         tech_stack: string,         date_founded: date,         image: //what put here?     },     social_media: {         blog: string,         twitter: string,         facebook: string,         linkedin: string,         email: string,     },     about_founder: {         founder_name: string,         social_media_founder: string     } });    module.exports = mongoose.model("startup", startupschema); 

then route is:

app.js  // create add new startup database router.post("/new", function(req, res) {    // data form    var about_startup = {        startup_name: req.body.startupname,        startup_url: req.body.url,        short_description: req.body.shortdescription,        long_description: req.body.longdescription,        tech_stack: req.body.techstack,        date_founded: req.body.foundeddate,     };    var social_media = {        blog: req.body.blog,        twitter: req.body.twitter,        facebook: req.body.facebook,        linkedin: req.body.linkedin,        email: req.body.email    };    var about_founder = {         founder_name: req.body.foundername,         social_media_founder: req.body.foundersocialmedia    };     //pass data through | write better explaination later    var newstartup = {about_startup: about_startup, social_media: social_media, about_founder: about_founder};    startup.create(newstartup, function(err, newlycreatedstartup){        if(err){            console.log(err);        } else {            // redirect show page            res.redirect("/startups");        }    }); }); 

this code works want other upload file. html input image/file upload is:

<input class="form-control" type="file" name="image" accept=".jpg"> 

image: //what put here? 

you can use string

why string?

  • you can convert image base64 string ease
  • the string can used render image in html
  • the string can re-converted file

jsfiddle : convert image base64 string


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -