Nodejs+ajax enables simple file upload functionality

Source: Internet
Author: User

App.js introduces the third-party middleware Multer to implement the file upload function.

var multer  = require (' Multer ');   Use Express's third-party middleware Multer to implement the file upload function. App.use (Multer ({// settings file uploaded to location  dest: './public/images/upload ',    function  (fieldname, filename) {    return  filename;  }});

Routing

function (req, res) {        Req.flash (' success ', ' File upload succeeded! ') );        Res.json ({"result": {message: "File Upload succeeded!") }});    });

Html

<type= "file"  ID= "Photo"><  ID = "Upload" > upload </button>

Ajax

    /*Uploading Files*/        $("#upload"). Click (function(){            //Do somethingvar file = $ ("#photo") [0].files; //Last Modified TimeConsole.log ("File Last Modified" +file[0].lastmodifieddate); //nameConsole.log ("File name" +file[0].name); //size byteConsole.log ("File Size" +file[0].size); //typeConsole.log ("File type" +file[0].type); //Create a Formdata object            vardata =NewFormData (); //adding data to a Formdata object$.each (file,function(i, file) {Data.append (' Upload_file ', file);            }); $.ajax ({data:data, url:'/upload ', type:"POST", DataType:' JSON ', ContentType:false,//not missingProcessData:false,//not missingSuccessfunction(data) {Console.log (data); Alert ("Upload success!" "); }, Error:function(Err) {Console.log (' ERROR: ' +err);        }            }); })

Nodejs+ajax enables simple file upload functionality

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.