Upload Files in ASP.NET at Production server

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags , , , with Leave a comment on Upload Files in ASP.NET at Production server

In this article, i am going to demonstrate that how to upload the file in ASP.NET production server.

Most of the case, a developer created a code to upload the file and test it on his local machine. program runs smoothly, but as he forward the same code on production. He stuck in the file permission error as on the local he has the full permission whereas on development its not the case.

So i decided to write this tutorial for the beginners which will demonstrate lots of ASP.NET features.

First we need to create a folder in Server and share it with ASP.NET web server as described.

Right click on folder, select Web sharing tab and select the radio button “Share this folder”. One  window appear, give it alias name which will be used in coding.

After this, open Run box and type “inetmgr“. you will see that your web shared folder appears in the IIS.

Right click on folders property. one window will opened.

Select “Directory Security” tab then click on “Edit” button and then check “Anonymous access”. And because of this setting any body can access or download that file.

Now we are ready, to create a sample application which will demonstrate that how to upload the file and use it.

Create ASP.NET project in Visual Studio. Create a webpage with fileupload, hyperlink and button control.

<form id="form1" runat="server">
<asp:FileUpload ID="uploadFile" runat="server" /><br />
<asp:HyperLink ID="hypLink" runat="server" Visible="false" Target="_blank"></asp:HyperLink><br />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" />
</form>

In web.config add two keys.  One for actual folder location in which we are going to upload the files and other its alias name. we can also hard code that, but its good practice to make it configurable at any time because in near future we may want to change the location.


<appSettings>

<add key="DemoAttachment" value="D:ShareDemoShare"></add>

<add key="DemoAttachmentVirtualPath" value="http://localhost/DemoShare/"></add>

</appSettings>

In DemoAttachmentVirtualPath key, value should like “http://machinename/” + Alias folder name.

On click event of button, write below code:

string strPath = ConfigurationManager.AppSettings["DemoAttachment"];
uploadFile.SaveAs(strPath + System.IO.Path.GetFileName(uploadFile.FileName));
hypLink.Visible = true;
string VirtualPath = ConfigurationManager.AppSettings["DemoAttachmentVirtualPath"];
hypLink.Text = System.IO.Path.GetFileName(uploadFile.FileName);
hypLink.NavigateUrl = VirtualPath + uploadFile.FileName;
string strPath = ConfigurationManager.AppSettings["DemoAttachment"];

uploadFile.SaveAs(strPath + System.IO.Path.GetFileName(uploadFile.FileName));
hypLink.Visible = true;
string VirtualPath = ConfigurationManager.AppSettings["DemoAttachmentVirtualPath"];

hypLink.Text = System.IO.Path.GetFileName(uploadFile.FileName);

hypLink.NavigateUrl = VirtualPath + uploadFile.FileName;

The output will look like:

Download Source code

For the file size limitation, please visit this article : https://jitendrazaa.com/blog/?p=125

cannot access a closed file – FileUpload in ASP.NET

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags , with 16 Comments on cannot access a closed file – FileUpload in ASP.NET

This was very interesting error, i got during development of file upload control in ASP.NET.

On my local system, every thing was just fine. but when i deployed my application on development server, my control was able to upload only small size files.

whenever i tried to upload large size file i got error “cannot access a closed file”

After going through MSDN i found that there is one new element added in ASP.NET 2.0 known as httpRuntime.

So i just added this new Tag in my web.config and every thing was right.

<system.web>
<httpRuntime  maxRequestLength="15360" requestLengthDiskThreshold="15360"/>
</system.web>

Note :

requestLengthDiskThreshold should not exceed the maxRequestLengthattribute.

To read more about this, please visit http://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx

Style File upload control in ASP / HTML

Author posted by Jitendra on Posted on under category Categories HTML and tagged as Tags , , , with 2 Comments on Style File upload control in ASP / HTML

As most of us know that we cannot style the file upload control to much level, still we can give it a stunning effect with the help of CSS.

There is nothing in code to explain.

I took three upload control and one button to give effect.

Lets say upload control is in ASP.net or in HTML.

<asp:FileUpload ID="FileUpload1" CssClass="Cntrl1" runat="server" />
Or
html file upload
<input id="readFile" type="file" runat="server" class="Cntrl1" />

Then create a simple CSS class of name “Cntrl1”.

  <style id="cssStyle" type="text/css" media="all">
       .Cntrl1
      {
       background-color:#abcdef;
       color: Yellow;
       border: 1px solid #AB00CC;
       font: Verdana 10px;
       padding: 1px 4px;
       font-family: Palatino Linotype, Arial, Helvetica, sans-serif;
      }
</style>

Out put in different browsers will look like :

Output in Chrom
Output in Chrom
Output in Mozilla
Output in Mozilla
Output in Internet explorer
Output in Internet explorer

View Demo of FileUpload