Uploading Files Using Ajax AsyncFileUploader In ASP.Net

Background
 
In ASP.NET FileUploader is a very lengthy process to upload files asynchronously because we need to write many lines of code. Using the Ajax AsyncFileUpload control however we can upload files asynchronously without much code. So let us learn about the AsyncFileUpload control step-by-step.
 
AsyncFileUpload Control
 
AsyncFileUpload is an ASP.Net Ajax control that allows you to asynchronously upload files to the server. The file uploading results can be checked both in the server and client sides.
 
The following are some of the common properties of the ASP.Net Ajax asyncFileUpload control:  
  • FileBytes: Gets an array of the bytes in a file.
  • FileContent: Gets a System.IO.Stream object that points to a file to upload.
  • FileName: Gets the name of a file on a client to upload.
  • HasFile: Gets a value indicating whether the control contains a file.
  • PostedFile: Gets the underlying System.Web.HttpPostedFile object for a file.
  • CompleteBackColor : Control's background color on upload complete.

  • ErrorBackColor: Control's background color on on error.

 

  • UploadingBackColor: Control's background color on uploading  file. 
  • PersistFile : Decides whether the files persist or not with true and false properties according to the PersistedStoredType.
  • PersistedStoreType : Decides where to persist a file such as session or other.

 

The following are the methods of the ASP.Net File Upload control:

  • ClearAllFilesFromPersistedStore.
  • ClearAllFilesFromPersistedStore.
  • ClearFileFromPersistedStore.
  •  CreateChildControls.
  •  CreateControlStyle.
  • DescribeComponent .
  • GenerateHtmlInputFileControl.
  • GenerateHtmlInputHiddenControl.
  • GetBytesFromStream.
  •   OnPreRender.
  • OnUploadedComplete.
  • OnUploadedFileError.
  •  SaveAs.

Now let us see the preceding explanation by creating a sample web application as follows:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".

  2. "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).

  3. Provide the web site a name such as "UsingAsyncFileuploader" or another as you wish and specify the location.

  4. Then right-click on the Solution Explorer and select "Add New Item" and Add Web Form.

  5. Drag and drop one Button, a Label and a UsingAsyncFileuploader control onto the <form> section of the Default.aspx page from the Ajax Control Toolkit.

  6. Drag and drop scriptManager.

Now the default.aspx page source code will looks as follows.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  4. <!DOCTYPE html>  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head id="Head1" runat="server">  
  7.     <title>Article by Vithal Wadje</title>  
  8. </head>  
  9. <body bgcolor="grey">  
  10.     <form id="form1" runat="server">  
  11.     <asp:ScriptManager ID="ScriptManager1" runat="server">  
  12.     </asp:ScriptManager>  
  13.     <br />  
  14.     <br />  
  15.     <div style="color: white">  
  16.         <table>  
  17.             <tr>  
  18.                 <td>  
  19.                     Select Files  
  20.                 </td>  
  21.                 <td>  
  22.                     <asp:AsyncFileUpload Width="280px" ID="AsyncFileUpload1" runat="server" />  
  23.                 </td>  
  24.             </tr>  
  25.             <tr>  
  26.                 <td>  
  27.                 </td>  
  28.                 <td>  
  29.                     <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />  
  30.                 </td>  
  31.             </tr>  
  32.         </table>  
  33.     </div>  
  34.     <asp:Label ID="Label1" runat="server" ForeColor="LawnGreen" Text=" "></asp:Label>  
  35.     </form>  
  36. </body>  
  37. </html> 
Create the folder in Solution Explorer by right-clicking to save the uploaded files as in the following:
 
 
Write the following code for the Upload button click event to upload and save files on the server folder as in the following:
  1. protected void btnUpload_Click(object sender, EventArgs e)  
  2.    {  
  3.        if(AsyncFileUpload1.HasFile)  
  4.        {  
  5.           String getFileName = Path.GetFileName(AsyncFileUpload1.FileName);    
  6.            AsyncFileUpload1.SaveAs(Server.MapPath("~/UploadedFiles/"+getFileName));  
  7.            Label1.Text = "File Uploaded Successfull";  
  8.        }  
  9.    } 
The entire code of the default.aspx.cs page will look as follows:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8.   
  9. public partial class _Default : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.        
  14.     }  
  15.     protected void btnUpload_Click(object sender, EventArgs e)  
  16.     {  
  17.         if(AsyncFileUpload1.HasFile)  
  18.         {  
  19.            String getFileName = Path.GetFileName(AsyncFileUpload1.FileName);    
  20.             AsyncFileUpload1.SaveAs(Server.MapPath("~/UploadedFiles/"+getFileName));  
  21.             Label1.Text = "File Uploaded Successfull";  
  22.         }  
  23.     }  

Now run the application. The UI will look such as follows:
 
 
Now click on the Browse Button and select file as in the following:
 
 
Now click on the Upload button. The following message is shown as I have set to a label control as in the following:
 
 
Now refresh the folder we created to save the files to, it will look such as follows:
 
 
 
Now you have seen how to upload files using the ASP.Net Ajax AsyncFileUploader.
Notes
  • Do a proper validation such as if it has a file or not of the File Upload control when implementing.

  • For more details and explanation, download the Uploaded Zip file.

  • Download Ajax Control toolkit.
Summary

From all the preceding examples you have learned how to upload files using the ASP.Net Ajax AsyncFileUploader. I hope this article is useful for all readers, if you have a suggestion then please contact me.


Similar Articles