File Uploading and Downloading in ASP.NET

Discussion in 'ASP.NET' started by sanjitsil, Dec 14, 2008.

  1. sanjitsil

    sanjitsil New Member

    Joined:
    Dec 9, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Lead Software Engineer in an MNC
    Location:
    Hyderabad

    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:

    [​IMG]

    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:
    HTML:
    <input id="FileUpload" type="file" runat="server"/>
    Step II: Add enctype="multipart/form-data" in form tag:
    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:
    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:

    [​IMG]

    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:
    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:
    <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:
    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:

    [​IMG]

    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
     
    Last edited by a moderator: Jan 21, 2017
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Nice and simple article, well explained!
     
  3. NDL

    NDL New Member

    Joined:
    Oct 20, 2008
    Messages:
    71
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    SL,colombo
    Home Page:
    http://www.nisal.co.nr
    nice work!!
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice