Click here to Skip to main content
15,867,308 members
Articles / Web Development / HTML
Article

Simple AJAX File Upload

Rate me:
Please Sign up or sign in to vote.
4.30/5 (53 votes)
29 Dec 2006CPOL2 min read 650.6K   12.5K   101   97
A simple approach to enable the FileUpload control to work with Microsoft AJAX.

Introduction

AJAX is a great thing for ASP.NET developers. It has begun to level the playing field with rich client applications, in terms of user experience and responsiveness. One downside has been that many things that once worked are broken; including the FileUpload control.

The essence of the problem is that the FileUpload control does not work with asynchronous postbacks, and therefore does not work from within an AJAX UpdatePanel. The technique presented in this article allows the FileUpload control to work within an UpdatePanel, by forcing a full postback; however, the rest of the controls can still take advantage of the asynchronous postbacks provided by the UpdatePanel. The example application will be a simple web form containing a DatePicker Control, a Label and a FileUpload. We will see that the FileUpload will function via a full postback, but the DatePicker will retain its AJAX behaviour.

1. Create a New AJAX-enabled Web Project

When Microsoft AJAX is installed, it adds a new template to Visual Studio, ASP.NET AJAX-Enabled Web Site. Create a new one of these:

Create a new AJAX Web Site

2. Create a New Web Form

In this example, I have added an Eclipse Web Solutions ASP.NET DatePicker control, a Label, and a FileUpload control, all within an UpdatePanel:

Eclipse Web Solutions ASP.NET DatePicker

HTML
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
   <ContentTemplate>
      <ews:DatePicker ID="DatePicker1"
         runat="server" UsingUpdatePanel="True" /><br />
      <asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
      <asp:FileUpload ID="FileUpload1" runat="server" />
      <asp:Button ID="Button1" runat="server"
         Text="Upload" />
   </ContentTemplate>
</asp:UpdatePanel>

Code-behind:

C#
protected void DatePicker1_SelectionChanged(object sender, EventArgs e)
{
    Label1.Text = DatePicker1.DateValue.ToShortDateString();
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        Label1.Text = FileUpload1.FileName;
    }
}

It is at this point that we experience the problem. Run the form, and you will find that the file upload control does not work. Because the file upload control is within an UpdatePanel, the file is not posted to the server.

3. Enable File Upload Full Postback

As mentioned earlier, the trick is to force the file upload control to perform a full postback, and we do this using triggers. Triggers allow the developer to specify what will cause partial and full postbacks. They must be defined within the UpdatePanel but outside of the ContentTemplate. We want to create a trigger that will instruct the button that we are using for the upload to perform a full postback. The updated markup is:

HTML
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
 <Triggers>
  <asp:PostBackTrigger ControlID="Button1" />
 </Triggers>
 <ContentTemplate>
  <ews:DatePicker ID="DatePicker1" runat="server" UsingUpdatePanel="True" 
                  OnSelectionChanged="DatePicker1_SelectionChanged" /><br />
  <asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
  <asp:FileUpload ID="FileUpload1" runat="server" />
  <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
 </ContentTemplate>
</asp:UpdatePanel>

Now, when the form is run, the DatePicker is still working asynchronously, but the file upload is also working, thanks to the PostBackTrigger.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
Liam McLennan has been developing for the internet since 2001. He is passionate about delivering top quality I.T. solutions that address his customer's needs.

Comments and Discussions

 
GeneralRe: UpdateProgress Problem Pin
Thomas Maule1-Feb-10 7:43
Thomas Maule1-Feb-10 7:43 
AnswerRe: UpdateProgress Problem Pin
Thomas Maule2-Feb-10 5:44
Thomas Maule2-Feb-10 5:44 
GeneralRe: UpdateProgress Problem Pin
Member 353020313-Apr-11 20:52
Member 353020313-Apr-11 20:52 
Generalproblem in file upload Pin
Shweta parikh19-Mar-09 22:53
Shweta parikh19-Mar-09 22:53 
GeneralMy vote of 1 Pin
danny_pow23-Jan-09 0:12
danny_pow23-Jan-09 0:12 
GeneralNeed Help.. Pin
ji_vijay29-Oct-08 18:05
ji_vijay29-Oct-08 18:05 
GeneralRe: Need Help.. Pin
liammclennan29-Oct-08 18:07
liammclennan29-Oct-08 18:07 
GeneralGreat Article Pin
Member 446143322-Oct-08 2:27
Member 446143322-Oct-08 2:27 
GeneralAlso important (at least UserControl) Pin
ph8121-Oct-08 20:09
ph8121-Oct-08 20:09 
GeneralRe: Also important (at least UserControl) Pin
gezequiel0930-Oct-08 8:15
gezequiel0930-Oct-08 8:15 
GeneralRe: Also important (at least UserControl) Pin
shogedal8-Feb-10 3:49
shogedal8-Feb-10 3:49 
QuestionSo,where is ajax here? Pin
ab_dc11-Aug-08 3:39
ab_dc11-Aug-08 3:39 
AnswerRe: So,where is ajax here? Pin
liammclennan11-Aug-08 11:15
liammclennan11-Aug-08 11:15 
AnswerRe: So,where is ajax here? Pin
Portatofe2-Oct-08 5:09
Portatofe2-Oct-08 5:09 
AnswerRe: So,where is ajax here? Pin
villecoder30-Jan-09 10:22
villecoder30-Jan-09 10:22 
GeneralFile Upload Pin
HendrikO17-Jun-08 1:12
HendrikO17-Jun-08 1:12 
QuestionUpdate Panel Propblems. Pin
Hasan Mohiuddin Farooqi18-May-08 20:06
Hasan Mohiuddin Farooqi18-May-08 20:06 
QuestionWhat about a file upload control inside an ASCX user control inside an update panel Pin
frankkirchner1-May-08 13:21
frankkirchner1-May-08 13:21 
AnswerRe: What about a file upload control inside an ASCX user control inside an update panel Pin
liammclennan1-May-08 13:41
liammclennan1-May-08 13:41 
GeneralRe: What about a file upload control inside an ASCX user control inside an update panel Pin
frankkirchner1-May-08 15:28
frankkirchner1-May-08 15:28 
AnswerSolution for using this technique with a user control Pin
kbomb98724-Jun-08 4:24
kbomb98724-Jun-08 4:24 
GeneralRe: Solution for using this technique with a user control Pin
aikipoodle20-Jul-08 9:51
aikipoodle20-Jul-08 9:51 
GeneralRe: Solution for using this technique with a user control Pin
liaras7-Aug-09 10:10
liaras7-Aug-09 10:10 
GeneralRe: Solution for using this technique with a user control Pin
princerc14-Feb-09 3:26
princerc14-Feb-09 3:26 
QuestionIs it possible to call upload from a menu item? Pin
alan9317-Mar-08 3:11
alan9317-Mar-08 3:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.