Slip 8 - A) Using Node.js create a web page to read two file names from user and combine in third file with all content in Upper case.

Solution:

 Code for app.js


var express = require("express");

var multer = require('multer'); 

var app = express(); 

var storage = multer.diskStorage({ 

destination: function (req, file, callback) { 

callback(null, './uploads'); 

}, 

filename: function (req, file, callback) { 

callback(null, file.originalname); 


}); 

var upload = multer({ storage : storage}).single('myfile'); 

app.get('/',function(req,res){ 

res.sendFile(__dirname + "/index1.html"); 

}); 

app.post('/upload',function(req,res){ 

upload(req,res,function(err) { 

if(err) { 

return res.end("Error uploading file."); 


res.end("File is uploaded successfully!"); 

}); 

}); 

app.listen(2000,function(){ 

console.log("Server is running on port 2000"); 

}); 


Code for index.html


<html>

<head>

<title>File upload in Node.js </title>

<script

src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script

src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js"><

/script>

<script>

$(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; 

}); 

}); 

</script>

</head>

<body>

<h1>Express.js File Upload</h1>

<form id="uploadForm" enctype="multipart/form-data" action="/upload"

method="post">

<input type="file" name="myfile" /><br/><br/>

<input type="submit" value="Upload Image" name="submit"><br/><br/>

<span id="status"></span>

</form>

</body>

</html>

Post a Comment

0 Comments