<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Multer Package &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/multer-package/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Tue, 10 Jul 2018 07:46:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Multer Package &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Upload Files Or Images To Server Using Node JS</title>
		<link>https://mail.sibeeshpassion.com/upload-files-or-images-to-server-using-node-js/</link>
					<comments>https://mail.sibeeshpassion.com/upload-files-or-images-to-server-using-node-js/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sat, 10 Dec 2016 12:43:33 +0000</pubDate>
				<category><![CDATA[Node JS]]></category>
		<category><![CDATA[Express JS Package]]></category>
		<category><![CDATA[Multer Package]]></category>
		<category><![CDATA[Server Side Upload Using Node JS]]></category>
		<category><![CDATA[Upload in Node JS]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11967</guid>

					<description><![CDATA[[toc] Introduction In this post, we will see how we can upload files or images to the server using Node JS. Here we are going to use Visual Studio for our development and preceding NPM packages for our easy development. express multer body-parser We will briefly explain the use of these packages. As you all know Node JS is a runtime environment built on Chrome&#8217;s V8 JavaScript engine for server-side and networking application. And it is an open source which supports cross platforms. Node JS applications are written in pure JavaScript. If you are new to Node JS, I strongly [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>In this post, we will see how we can upload files or images to the server using <a href="http://sibeeshpassion.com/category/Node-JS/" target="_blank" rel="noopener">Node JS</a>. Here we are going to use <a href="http://sibeeshpassion.com/category/visual-studio/" target="_blank" rel="noopener">Visual Studio</a> for our development and preceding NPM packages for our easy development.</p>
<ul>
<li>express</li>
<li>multer</li>
<li>body-parser</li>
</ul>
<p>We will briefly explain the use of these packages. As you all know Node JS is a runtime environment built on Chrome&#8217;s V8 JavaScript engine for server-side and networking application. And it is an open source which supports cross platforms. Node JS applications are written in pure <a href="http://sibeeshpassion.com/category/javascript/" target="_blank" rel="noopener">JavaScript</a>. If you are new to Node JS, I strongly recommend you to read my previous posts about Node JS <a href="http://sibeeshpassion.com/category/node-js/" target="_blank" rel="noopener">here</a>.</p>
<h2><strong>Download source code</strong></h2>
<p>You can always download the source code from MSDN <a href="https://code.msdn.microsoft.com/Upload-Files-Or-To-Server-15f69aaa" target="_blank" rel="noopener">Upload Files Or Images To Server Using Node JS</a> or you can clone from <a href="https://github.com/SibeeshVenu/NodeJS_File_Upload">Github</a></p>
<h2><strong>Background</strong></h2>
<p>A few years back if you need to upload any files or images to the server, you were completely depended on server side languages like <a href="http://sibeeshpassion.com/category/csharp/" target="_blank" rel="noopener">C#</a> and <a href="http://sibeeshpassion.com/category/PHP/" target="_blank" rel="noopener">PHP</a>. Everything is changed after the revolution of <a href="http://sibeeshpassion.com/category/node-js/" target="_blank" rel="noopener">Node JS</a>. Here I will show you how to upload the files to the server using Node JS, without writing even a single line of server-side code. I hope you will like this.</p>
<h2>Using the code</h2>
<h3><strong>Create a Blank Node JS Web Application</strong></h3>
<p>Create a blank Node JS web application.</p>
<div id="attachment_11968" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/12/New_Node_JS_Web_Application.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-11968" class="size-large wp-image-11968" src="http://sibeeshpassion.com/wp-content/uploads/2016/12/New_Node_JS_Web_Application-1024x709.png" alt="new_node_js_web_application" width="634" height="439" srcset="/wp-content/uploads/2016/12/New_Node_JS_Web_Application-1024x709.png 1024w, /wp-content/uploads/2016/12/New_Node_JS_Web_Application-300x208.png 300w, /wp-content/uploads/2016/12/New_Node_JS_Web_Application-768x532.png 768w, /wp-content/uploads/2016/12/New_Node_JS_Web_Application-160x110.png 160w, /wp-content/uploads/2016/12/New_Node_JS_Web_Application-400x277.png 400w, /wp-content/uploads/2016/12/New_Node_JS_Web_Application-866x600.png 866w, /wp-content/uploads/2016/12/New_Node_JS_Web_Application.png 515w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11968" class="wp-caption-text">new_node_js_web_application</p></div>
<h3><strong>Set Up Dependencies in package.json</strong></h3>
<p>To get started, we will set up our dependencies first. To do so, please open your package.json file and paste the preceding code.</p>
<p>[js]<br />
{<br />
&#8220;name&#8221;: &#8220;node_js_file_upload&#8221;,<br />
&#8220;version&#8221;: &#8220;0.0.1&#8221;,<br />
&#8220;description&#8221;: &#8220;Node_JS_File_Upload&#8221;,<br />
&#8220;main&#8221;: &#8220;server.js&#8221;,<br />
&#8220;dependencies&#8221;: {<br />
&#8220;body-parser&#8221;: &#8220;^1.15.2&#8221;,<br />
&#8220;express&#8221;: &#8220;^4.14.0&#8221;,<br />
&#8220;multer&#8221;: &#8220;^1.2.0&#8221;<br />
},<br />
&#8220;author&#8221;: {<br />
&#8220;name&#8221;: &#8220;Sibeesh&#8221;<br />
}<br />
}<br />
[/js]</p>
<p>Now, run the NPM install command as follows.</p>
<p>[csharp]<br />
npm install<br />
[/csharp]</p>
<p>Once you run the command, you can see that the dependencies are installed in the solution.</p>
<div id="attachment_11969" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/12/NPM-Packages-e1481366627834.png"><img decoding="async" aria-describedby="caption-attachment-11969" class="size-full wp-image-11969" src="http://sibeeshpassion.com/wp-content/uploads/2016/12/NPM-Packages-e1481366627834.png" alt="npm-packages" width="634" height="624" srcset="/wp-content/uploads/2016/12/NPM-Packages-e1481366627834.png 363w, /wp-content/uploads/2016/12/NPM-Packages-e1481366627834-300x295.png 300w, /wp-content/uploads/2016/12/NPM-Packages-e1481366627834-400x394.png 400w, /wp-content/uploads/2016/12/NPM-Packages-e1481366627834-610x600.png 610w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11969" class="wp-caption-text">npm-packages</p></div>
<p>Now we can understand what are these dependencies used for.</p>
<ul>
<li>express</li>
</ul>
<p>As per the <a href="https://expressjs.com/" target="_blank" rel="noopener">Express Team</a>, Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love. You can always learn more about Express Package <a href="https://expressjs.com/" target="_blank" rel="noopener">here</a></p>
<ul>
<li>multer</li>
</ul>
<p>Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files. It is written on top of busboy for maximum efficiency. Please read more about multer package <a href="https://github.com/expressjs/multer" target="_blank" rel="noopener">here.</a></p>
<h3><strong>Start using our dependencies</strong></h3>
<p>You can create the instances of our dependencies as follows.</p>
<p>[js]<br />
var Express = require(&#8216;express&#8217;);<br />
var multer = require(&#8216;multer&#8217;);<br />
var bodyParser = require(&#8216;body-parser&#8217;);<br />
var app = Express();<br />
app.use(bodyParser.json());<br />
[/js]</p>
<p>Then, it is time to create a storage which says where and how the files/images should be saved.</p>
<p>[js]<br />
var Storage = multer.diskStorage({<br />
destination: function (req, file, callback) {<br />
callback(null, &#8220;./Images&#8221;);<br />
},<br />
filename: function (req, file, callback) {<br />
callback(null, file.fieldname + &#8220;_&#8221; + Date.now() + &#8220;_&#8221; + file.originalname);<br />
}<br />
});<br />
[/js]</p>
<p>Each file contains the following information:</p>
<p><em>fieldname :</em> Field name specified in the form<br />
<em>originalname :</em> Name of the file on the user&#8217;s computer<br />
<em>encoding :</em> Encoding type of the file<br />
<em>mimetype :</em> Mime type of the file<br />
<em>size :</em> Size of the file in bytes<br />
<em>destination :</em> The folder to which the file has been saved<br />
<em>filename :</em> The name of the file within the destination<br />
<em>path :</em> The full path to the uploaded file<br />
<em>buffer :</em> A Buffer of the entire file</p>
<p>Now please create multer object as follows.</p>
<p>[js]<br />
var upload = multer({ storage: Storage }).array(&#8220;imgUploader&#8221;, 3); //Field name and max count<br />
[/js]</p>
<p>Here multer accepts the storage we created in our previous step as the parameter. The function [js]array(fieldname[, maxCount])[/js]<br />
accept an array of files, all with the name fieldname.</p>
<p>Now it is time to write our post and get action.</p>
<p>[js]<br />
app.get(&#8220;/&#8221;, function (req, res) {<br />
res.sendFile(__dirname + &#8220;/index.html&#8221;);<br />
});</p>
<p>app.post(&#8220;/api/Upload&#8221;, function (req, res) {<br />
upload(req, res, function (err) {<br />
if (err) {<br />
return res.end(&#8220;Something went wrong!&#8221;);<br />
}<br />
return res.end(&#8220;File uploaded sucessfully!.&#8221;);<br />
});<br />
});<br />
[/js]</p>
<p>Here <em>/api/Upload</em> is the action name we are going to set in out HTML page which we will create soon. And last but not the least, we need to make sure that the app is listening to our particular port, in this case it is port 2000.</p>
<p>[js]<br />
app.listen(2000, function (a) {<br />
console.log(&#8220;Listening to port 2000&#8221;);<br />
});<br />
[/js]</p>
<h3><strong>Create HTML page and set up uploading</strong></h3>
<p>You can create the page as follows with the references of the <em>jquery-3.1.1.min.js</em> and <em>jquery.form.min.js</em>.</p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;</p>
<p>&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221;&gt;<br />
&lt;head&gt;<br />
&lt;meta charset=&#8221;utf-8&#8243; /&gt;<br />
&lt;title&gt;Upload images to server using Node JS&lt;/title&gt;<br />
&lt;script src=&#8221;Scripts/jquery-3.1.1.min.js&#8221;&gt;&lt;/script&gt;<br />
&lt;script src=&#8221;Scripts/jquery.form.min.js&#8221;&gt;&lt;/script&gt;</p>
<p>&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;form id=&#8221;frmUploader&#8221; enctype=&#8221;multipart/form-data&#8221; action=&#8221;api/Upload/&#8221; method=&#8221;post&#8221;&gt;<br />
&lt;input type=&#8221;file&#8221; name=&#8221;imgUploader&#8221; multiple /&gt;<br />
&lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; id=&#8221;btnSubmit&#8221; value=&#8221;Upload&#8221; /&gt;<br />
&lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>Please be noted that the <em>ecctype </em>for your form must be <em>multipart/form-data</em> and the action must be same as we set in our API.</p>
<h3><strong>Create Ajax submit event</strong></h3>
<p>Now it is time to create our ajax event where we are going to call our API.</p>
<p>[js]<br />
&lt;script&gt;<br />
$(document).ready(function () {<br />
var options = {<br />
beforeSubmit: showRequest, // pre-submit callback<br />
success: showResponse // post-submit callback<br />
};</p>
<p>// bind to the form&#8217;s submit event<br />
$(&#8216;#frmUploader&#8217;).submit(function () {<br />
$(this).ajaxSubmit(options);<br />
// always return false to prevent standard browser submit and page navigation<br />
return false;<br />
});<br />
});</p>
<p>// pre-submit callback<br />
function showRequest(formData, jqForm, options) {<br />
alert(&#8216;Uploading is starting.&#8217;);<br />
return true;<br />
}</p>
<p>// post-submit callback<br />
function showResponse(responseText, statusText, xhr, $form) {<br />
alert(&#8216;status: &#8216; + statusText + &#8216;\n\nresponseText: \n&#8217; + responseText );<br />
}<br />
&lt;/script&gt;<br />
[/js]</p>
<blockquote><p><em>ajaxSubmit</em> function is part of the plugin <em>jquery.form.min.js</em>, so please make sure that you have included it.</p></blockquote>
<h3><strong>Run your application</strong></h3>
<p>Now please run your application. Before running your application, you can always set your script file as your start up file, to set it, please right click on your project and click on properties.</p>
<div id="attachment_11970" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/12/Set-start-up-file.png"><img decoding="async" aria-describedby="caption-attachment-11970" class="size-large wp-image-11970" src="http://sibeeshpassion.com/wp-content/uploads/2016/12/Set-start-up-file-1024x383.png" alt="set-start-up-file" width="634" height="237" srcset="/wp-content/uploads/2016/12/Set-start-up-file-1024x383.png 1024w, /wp-content/uploads/2016/12/Set-start-up-file-300x112.png 300w, /wp-content/uploads/2016/12/Set-start-up-file-768x288.png 768w, /wp-content/uploads/2016/12/Set-start-up-file-400x150.png 400w, /wp-content/uploads/2016/12/Set-start-up-file.png 1509w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11970" class="wp-caption-text">set-start-up-file</p></div>
<p>Now you can open your command prompt, you can manually locate your project in command prompt or you can use the &#8216;Open command prompt here&#8217; option. To select, please right click on your project and select the option as follows.</p>
<div id="attachment_11971" style="width: 783px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/12/Open_command_prompt_here_option.png"><img decoding="async" aria-describedby="caption-attachment-11971" class="size-full wp-image-11971" src="http://sibeeshpassion.com/wp-content/uploads/2016/12/Open_command_prompt_here_option-e1481369183917.png" alt="open_command_prompt_here_option" width="773" height="1008" srcset="/wp-content/uploads/2016/12/Open_command_prompt_here_option-e1481369183917.png 274w, /wp-content/uploads/2016/12/Open_command_prompt_here_option-e1481369183917-230x300.png 230w, /wp-content/uploads/2016/12/Open_command_prompt_here_option-e1481369183917-400x522.png 400w, /wp-content/uploads/2016/12/Open_command_prompt_here_option-e1481369183917-460x600.png 460w" sizes="(max-width: 773px) 100vw, 773px" /></a><p id="caption-attachment-11971" class="wp-caption-text">open_command_prompt_here_option</p></div>
<p>Now type <em>node server.js</em> in your command prompt which will make sure that your server is running. And if everything is fine, you can see a window as follows.</p>
<div id="attachment_11972" style="width: 593px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/12/Node_server_js_output.png"><img decoding="async" aria-describedby="caption-attachment-11972" class="size-full wp-image-11972" src="http://sibeeshpassion.com/wp-content/uploads/2016/12/Node_server_js_output.png" alt="node_server_js_output" width="583" height="414" srcset="/wp-content/uploads/2016/12/Node_server_js_output.png 583w, /wp-content/uploads/2016/12/Node_server_js_output-300x213.png 300w, /wp-content/uploads/2016/12/Node_server_js_output-400x284.png 400w" sizes="(max-width: 583px) 100vw, 583px" /></a><p id="caption-attachment-11972" class="wp-caption-text">node_server_js_output</p></div>
<p>We can run our webpage now as our server is ready, please go to your browser and type the URL as <a href="http://localhost:2000" target="_blank" rel="noopener">http://localhost:2000</a>. Select few files using the file uploader we have created.</p>
<div id="attachment_11973" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/12/Select_fiels_in_file_upload-e1481369767333.png"><img decoding="async" aria-describedby="caption-attachment-11973" class="size-full wp-image-11973" src="http://sibeeshpassion.com/wp-content/uploads/2016/12/Select_fiels_in_file_upload-e1481369767333.png" alt="select_fiels_in_file_upload" width="634" height="117" srcset="/wp-content/uploads/2016/12/Select_fiels_in_file_upload-e1481369767333.png 634w, /wp-content/uploads/2016/12/Select_fiels_in_file_upload-e1481369767333-300x55.png 300w, /wp-content/uploads/2016/12/Select_fiels_in_file_upload-e1481369767333-400x74.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11973" class="wp-caption-text">select_fiels_in_file_upload</p></div>
<p>If you click submit, you can see we are calling our method action and the files are uploaded.</p>
<div id="attachment_11974" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/12/File_uploaded_successfully_-e1481369870966.png"><img decoding="async" aria-describedby="caption-attachment-11974" class="size-full wp-image-11974" src="http://sibeeshpassion.com/wp-content/uploads/2016/12/File_uploaded_successfully_-e1481369870966.png" alt="file_uploaded_successfully_" width="634" height="122" srcset="/wp-content/uploads/2016/12/File_uploaded_successfully_-e1481369870966.png 634w, /wp-content/uploads/2016/12/File_uploaded_successfully_-e1481369870966-300x58.png 300w, /wp-content/uploads/2016/12/File_uploaded_successfully_-e1481369870966-400x77.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11974" class="wp-caption-text">file_uploaded_successfully_</p></div>
<div id="attachment_11975" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/12/Solution_explorer_window_after_saving_files-e1481369985988.png"><img decoding="async" aria-describedby="caption-attachment-11975" class="size-full wp-image-11975" src="http://sibeeshpassion.com/wp-content/uploads/2016/12/Solution_explorer_window_after_saving_files-e1481369985988.png" alt="solution_explorer_window_after_saving_files" width="634" height="603" srcset="/wp-content/uploads/2016/12/Solution_explorer_window_after_saving_files-e1481369985988.png 375w, /wp-content/uploads/2016/12/Solution_explorer_window_after_saving_files-e1481369985988-300x285.png 300w, /wp-content/uploads/2016/12/Solution_explorer_window_after_saving_files-e1481369985988-400x380.png 400w, /wp-content/uploads/2016/12/Solution_explorer_window_after_saving_files-e1481369985988-631x600.png 631w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11975" class="wp-caption-text">solution_explorer_window_after_saving_files</p></div>
<p>You can always download the source code attached to see the complete code and application. Happy coding!.</p>
<h2><strong>See also</strong></h2>
<ul>
<li><a href="https://www.npmjs.com/package/multer" target="_blank" rel="noopener">Multer Package for Node JS</a></li>
<li><a href="https://expressjs.com/" target="_blank" rel="noopener">Express JS Package for Node JS</a></li>
<li><a href="https://github.com/expressjs/body-parser" target="_blank" rel="noopener">Body Parser Package for Node JS</a></li>
</ul>
<h2><strong>Conclusion</strong></h2>
<p>Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h2><strong>Your turn. What do you think?</strong></h2>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/upload-files-or-images-to-server-using-node-js/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
