Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

File Upload Control with ASP.NET and AJAX

0.00/5 (No votes)
23 Aug 2012 1  
Problem: How to use your Asp.Net Fileupload control with AJAX.As everybody knows who uses ajax in his daily programming is, ajax don't postback the

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Problem: How to use your Asp.Net Fileupload control with AJAX.


As everybody knows who uses ajax in his daily programming is, ajax don't postback the whole page, AJAX allows the webpage to update Asynchronously.

Lots of us create our applications in asp.net and use fileuplod control to upload files on server side. In such case if we are using ajax with fileupload control, then we find out that null value in our fileupload control(empty HttpFileCollection), its because of the fileupload control which needs a complete postback to upload file.

The FileUpload control does not work inside an UpdatePanel control for uploading files using Asynchronous Postback. It is due to security imposed by browser which doesnot allow JavaScript to directly access the files in user system. But defenately, Synchronous Postback can be used to upload files. ASP.NET AJAX allows both Synchronous and Asynchronous Postback.

So,  "You cannot use Partial Postback to upload files on server, you have to use full post back".

Below is the Code:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
  <title>Untitled Page</title>
 </head>
 <body>
  <form id="form1" runat="server">
   <asp:ScriptManager ID="ScriptManager1" runat="server" />
   <div>
    <table width="50%" cellpadding="2" cellspacing="0">
     <tr>
      <td>
       <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
        <ContentTemplate>
         <asp:FileUpload ID="FileUpload1" runat="server" />
         <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
        </ContentTemplate>
        <Triggers>
         <asp:PostBackTrigger ControlID="btnUpload" />  
        </Triggers>
       </asp:UpdatePanel>
      </td>
     </tr>
    </table>
   </div>
  </form>  
 </body>
</html>

 

 

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(MapPath("~/TEST/" + FileUpload1.FileName));
        }
    }
}

Now, AJAX Asynchronous  File Upload Control is available to upload files with Post back.

REF:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here