Ajax on node.js and express -
hi trying file uploading(image) in node.js express.this far have done.
my app.js
var express = require('express'); //express web server var bodyparser = require('body-parser'); //connects bodyparsing middleware var formidable = require('formidable'); var path = require('path'); //used file path var fs =require('fs-extra'); //file system-needed renaming file etc var app = express(); app.use(express.static(path.join(__dirname, 'public'))); app.set('views', __dirname + '/views'); app.engine('html', require('ejs').renderfile); /* ========================================================== bodyparser() required allow express see uploaded files ============================================================ */ app.use(bodyparser({defer: true})); app.route('/').get(function(req,res) { console.log("server started!"); res.render('index.html'); res.end('done'); }); app.post('/upload', function(req, res) { var form = new formidable.incomingform(); form.parse(req, function(err, fields, files) { var targetpath= '/upload/' + files.imagefile.name; fs.move(files.imagefile.path, __dirname + '/upload/' + files.imagefile.name, function(err) { if (err) return console.log(err); console.log('moved successfully'); }); res.send('file uploaded ' + targetpath + ' - ' + files.imagefile.size + ' bytes'); }); }); var server = app.listen(3030, function() { console.log('listening on port %d', server.address().port); });
my html file:
<form action="/upload" method="post" enctype="multipart/form-data" id="uploadform"> <input name="imagefile" id="imageinput" type="file" /> <input type="submit" id="submit-btn" value="upload" /> <div id="filename"></div> <div id="filesize"></div> <div id="filetype"></div> <div id="progress"></div> <img src="images/ajax-loader.gif" id="loading-img" style="display:none;" alt="please wait"/> </form>
i able upload file.but want ajax without reloading page.please give information this.any appreciated.
Comments
Post a Comment