Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   File Uploading and Downloading in ASP.NET (http://www.go4expert.com/articles/file-uploading-downloading-aspnet-t15487/)

sanjitsil 14Dec2008 22:00

File Uploading and Downloading in ASP.NET
 

Uploading of File



When we are talking about uploading of file, we mean to say upload file physically and save into a particular location in server. We can also upload file that means save file into database. Let us first examine how we can upload file physically in a particular location in server. The code snippet has been written in c#.

Figure1:

http://www.go4expert.com/images/arti...p-net/fig1.png

In ASP.NET, we can do the file upload using HTML Input (File) control (as shown in above figure: Figure1). In order to start the process we need to follow the following basic steps:

Step I: Add the control:
Code: HTML

<input id="FileUpload" type="file" runat="server"/>

Step II: Add enctype="multipart/form-data" in form tag:
Code: HTML

<form id="frmUpload" enctype="multipart/form-data" runat="server">

After completing the abovementioned basic steps, now it is the time to write code to upload file into server (see Figure2):
Code: CSharp

FileUpload.PostedFile.SaveAs("D:\\FileStorage\\UploadedFile");

When we are saving file into server, we may need to validate few things depending on requirement. For example,
  • We may check file type and allow user to upload some specific file only;
  • We may allow user to upload upto a maximum size of file;
  • We may check the length of file and disallow user to upload empty file etc.

How to check file type?

Code:

System.IO.Path.GetExtension(FileUpload.PostedFile.FileName);//It  will give the extension of file and thus help to identify file type.
How to restrict file size?

Code:

if(FileUpload.PostedFile.ContentLength<=5242880) //5242880 bytes i.e., restricted upto 5mb
How to check empty file?

Code:

if(FileUpload.PostedFile.ContentLength>0) //File should not be empty by checking size of uploaded file in bytes
Figure2:

http://www.go4expert.com/images/arti...p-net/fig2.png

How to upload multiple file:



In order to upload multiple file we can simply take multiple input file control. But if we don't want to use multiple input file control instead keep one input file control only, here is the solution:

Code: CSharp

ArrayList arrayList = new ArrayList();
arrayList.Add(FileUpload);

foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in arrayList)
{
    HIF.PostedFile.SaveAs("D:\\FileStorage\\UploadedFile");
}

Notes: If there "access denied" error when trying to save files directly to server, we should check user write permissions. That means user should have write permissions in the folder where uploaded file is going to be saved. If we set maxRequestLength parameter in the <httpRuntime> section of the config file, file will be uploaded if the size is within the specified limit. Default is 4096 (4 megabytes). We can increase the length of maxRequestLength parameter. Microsoft recommends that we should use a maximum file size in the range of 10 to 20 megabytes (MB).
In web.config :
Code: CSharp

<httpRuntime maxRequestLength="20000"/>

Download of File:



Once we save the file into file storage server or into database, we need to think about download of the uploaded file .Here I am describing how we can download file from physical storage server (see Figure2):

Code: CSharp

protected void btnDownload_Click(object sender, EventArgs e)
{
    string fileName = "MyFile.txt";
    System.IO.FileStream fs = null;
    fs = System.IO.File.Open("D:\\FileStorage\\UploadedFile\\MyFile.txt ", System.IO.FileMode.Open);
    byte[] btFile = new byte[fs.Length];
    fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
    fs.Close();
    Response.AddHeader("Content-disposition", "attachment; filename=" + fileName);
    Response.ContentType = "application/octet-stream";
    Response.BinaryWrite(btFile);
    Response.End();
    fs = null;
}

When the abovementioned code will be executed an open save dialog box will appear to save the file in local system (see Figure3):

Figure3:

http://www.go4expert.com/images/arti...p-net/fig3.png

In my forthcoming article I will discuss how to save physical file into database.

Conclusion:



So we have seen upload of files and how to download uploaded file. Uploading and downloading files is very common in real life application

pradeep 18Dec2008 00:10

Re: File Uploading and Downloading in ASP.NET
 
Nice and simple article, well explained!

NDL 25Dec2008 14:26

Re: File Uploading and Downloading in ASP.NET
 
nice work!!

shabbir 6Jan2009 12:25

Re: File Uploading and Downloading in ASP.NET
 
Nomination for article of the month for December 2008 Started.


All times are GMT +5.5. The time now is 13:13.