Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


File Uploader

Add Comment
 

 
<div align="center"> <table border="0" width="70%" class="outline"> <tr> <td width="50%" class="outline"><b>Download File</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"><a class="wbox" href="file/fileuploader.zip">fileuploader.zip</a> (3kb)</td> <td width="50%" class="outline">.NET v1 (v1.0.3705)</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> While making websites which accept data from its users, sometimes there is a need to allow the user upload files to the Server. To solve this problem I have written this example to help to do just that. Even though its installation procedure might seem a bit difficult to new web developers, people who have done this before in any other language will find it very simple. Unlike ASP you do not need to purchase a separate component anymore, ASP.NET has inbuilt server-controls that make this job very easy for you. </p> <p><span class=wboxheado>Explanation</span><br> To upload files the <i>System.Web.UI.HtmlControls</i> namespace has a class called <b>HtmlInputFile</b> . This class exposes the properties of the traditional &lt;input type=&quot;file&quot;&gt; control.<br> <i>HtmlInputFile</i> class contains a property called <b>PostedFile</b> which returns an instance of the class <b>HttpPostedFile</b>. This class has various methods which help us to get various attributes of the posted file. Also the <i>HttpPostedFile</i> class exposes a method <b>SaveAs</b> which saves the posted file to your server.</p> <p><span class=wboxheado>Requirements</span><br> 1) .NET SDK v1.0.3705 (Note: This code might not run on future versions of the SDK)<br> 2) Web Server supporting ASP.NET with sufficient security permissions.</p> <p><span class=wboxheado>Installation Instructions:</span><br> 1) Unzip the zip file in to any directory (Remember to keep 'Use folder names' checked ON in your unzipping program.)<br> 2) Customize the look and color of the file by opening it in any text editor.<br> 3) Find out in which directory does your server allows <b> write</b> access to files (eg. on the www.brinkster.com server it is the '/db' directory ). <br> Without <b>write</b> access you cannot upload to any server.<br> If you server has some other directory then<br> Open 'FileUpload.aspx' file in a text editor and change the path of &quot;upload&quot;&nbsp;on to whatever path your server allows and save the file.<br> 4) Upload the all files to your server<br> 5) That's great you have setup the file upload page !!!!<br> 6) Then call the file <i>http://urserver.com/FileUpload.aspx</i> in your browser and upload your files...</p> <p><span class=wboxheado>Code</span><br> 1) <b> FileUpload.aspx</b> :- The File Upload Page</p> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre>&lt;%@ Import Namespace=&quot;System.IO&quot; %&gt; &lt;%@ page Language=&quot;C#&quot; debug=&quot;true&quot; %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;File Upload , By Saurabh Nandu - http://www.MasterCSharp.com&lt;/title&gt; &lt;script language=&quot;C#&quot; runat=&quot;server&quot;&gt; <span class=cmt>//This method is called when the 'upload' button is pressed</span> public void UploadFile(object sender , EventArgs E) { <span class=cmt>//check if the file posted is not null</span> if(myFile.PostedFile!=null) { <span class=cmt>//Get the filename only</span> string newnm = Path.GetFileName( myFile.PostedFile.FileName ) ; <span class=cmt>//save the file to the destination path on your server //change this path as per your needs //Remember the path you specify should exist and have 'write' access</span> myFile.PostedFile.SaveAs(Server.MapPath(&quot;upload&quot;) + &quot;\\&quot; + newnm) ; <span class=cmt>//Get the various properties of the Uploaded file</span> fname.Text=myFile.PostedFile.FileName; fenc.Text=myFile.PostedFile.ContentType ; fsize.Text=myFile.PostedFile.ContentLength.ToString(); } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;center&gt; &lt;h3&gt; File Upload Demo , by Saurabh Nandu &lt;/h3&gt; <span class=cmt>&lt;!-- It is very Important to specify enctype=&quot;multipart/form-data&quot; or the form won't work --&gt;</span> &lt;form method=&quot;post&quot; action=&quot;FileUpload.aspx&quot; enctype=&quot;multipart/form-data&quot; runat=&quot;server&quot; &gt; &lt;table border=&quot;1&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; &gt; &lt;tr&gt; &lt;td&gt;&lt;h5&gt;Select the File to upload&lt;/h5&gt;&lt;/td&lt;/tr&gt; &lt;tr&gt;&lt;td&gt; &lt;input type=&quot;file&quot; id=&quot;myFile&quot; runat=&quot;server&quot; &gt; &lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt; &lt;input type=&quot;button&quot; value=&quot;Upload&quot; OnServerClick=&quot;UploadFile&quot; runat=&quot;server&quot; &gt; &lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;br&gt; &lt;br&gt; &lt;table border=&quot;1&quot; cellspacing=&quot;2&quot;&gt; &lt;tr&gt;&lt;td&gt;&lt;b&gt;File Details&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;File Name :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;fname&quot; text=&quot;&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;File Encoding :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;fenc&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;File Size :(in bytes)&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;fsize&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/center&gt; &lt;/body&gt; &lt;/html&gt;</pre> </td> </tr> </table><P>&nbsp;</P> <P>2) A interesting manipulation you could do is to limit the size of the file uploaded to 1 mb. <BR> (Note: This code is not included in the downloaded code.) (Only relevant code shown) </P> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre>&lt;script language=&quot;C#&quot; runat=&quot;server&quot;&gt; <span class=cmt>//This method is called when the 'upload' button is pressed</span> public void UploadFile(object sender , EventArgs E) { <span class=cmt>//check if the file posted is not null //Say we want to limit the size of File uploaded by the //user to 1Mb (i.e.1048567bytes)</span> if(myFile.PostedFile!=null&amp;&amp;myFile.PostedFile.ContentLength&lt;=1048567) { <span class=cmt>//Get the filename only</span> string newnm = Path.GetFileName( myFile.PostedFile.FileName ) ; <span class=cmt>//save the file to the destination path on your server //change this path as per your needs //Remember the path you specify should exist and have 'write' access</span> myFile.PostedFile.SaveAs(Server.MapPath(&quot;upload&quot;) + &quot;\\&quot; + newnm) ; <span class=cmt>//Get the various properties of the Uploaded file</span> fname.Text=myFile.PostedFile.FileName; fenc.Text=myFile.PostedFile.ContentType ; fsize.Text=myFile.PostedFile.ContentLength.ToString(); } else { <span class=cmt>//Assume we have a label with the Id errormes to display the error</span> errormes.Text=&quot;You have uploaded a File which exceeds the 1mb Limit&quot; ; } &lt;/script&gt;</pre> </td> </tr> </table><p>You can also place multiple <i>HtmlInputFile</i> controls on the same page to allow your clients upload more than one file at a time. Another interesting property the <i>HttpPostedFile</i> object exposes is <b>InputStream</b>, which gets the <i>Stream</i> to the uploaded file, so incase you want to save the Uploaded File directly to the database, you can use this property and save the file directly to the binary field of the database.</p> <p>In order to protect you from <b>DOS</b> (Denial of Service) attacks Microsoft has by default limited the the size of the file that can be uploaded to under <i> 4 Mb</i>. In case you want to upload files larger than 4 Mb you will have to make specific changes in the <b>machine.config</b> (in order to affect all file upload scripts on the server) or the <b>web.config</b> (in order to affect just the file upload script in the current application) file. Locate or add the following section in the config file. The <b>maxRequestLength</b> is the property you should be updating to reflect the maximum file size that can be uploaded in bytes.<br> As you can see below the default is set to allow only 4 Mb ( 1 Mb = 1024 bytes ).<br> <br> &lt;Configuration&gt;<br> &nbsp; &lt;system.web&gt;<br> &nbsp;&nbsp;&nbsp; &lt;httpRuntime<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxRequestLength=&quot;4096&quot; /&gt;<br> &nbsp; &lt;/system.web&gt;<br> &lt;Configuration&gt;</p> <p><span class=wboxheado>Conclusion</span><br> This example demonstrates the power of the HtmlInputFile server-control that provides you with a flexible way of uploading files to the server.</p>

Comments

Add Comment