Click here to Skip to main content
Licence CPOL
First Posted 29 Dec 2006
Views 384,168
Downloads 6,001
Bookmarked 93 times

Simple AJAX File Upload

By | 29 Dec 2006 | Article
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

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

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:

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

About the Author

liammclennan

Web Developer

Australia Australia

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: Also important (at least UserControl) Pinmembershogedal3:49 8 Feb '10  
QuestionSo,where is ajax here? Pinmemberab_dc3:39 11 Aug '08  
AnswerRe: So,where is ajax here? Pinmemberliammclennan11:15 11 Aug '08  
AnswerRe: So,where is ajax here? PinmemberPortatofe5:09 2 Oct '08  
AnswerRe: So,where is ajax here? PinmemberZinkyu10:22 30 Jan '09  
GeneralFile Upload PinmemberHendrikO1:12 17 Jun '08  
QuestionUpdate Panel Propblems. PinmemberHasan Mohiuddin Farooqi20:06 18 May '08  
Hi Frnds,
 
I am facing some update panel problems while using a treeview control.
 
1) I set the expandDepth=1, and when the inner node is clicked entire tree is getting collapsed.
2) Giving PopulateOnDemand can not be set to true when then node is already set populateOnDemand to true. (This is becoz the child items r getting populated dynamically at runtime on click of node).
 
3) I have a download button in update panel when i clicked it, the exception Message is
 
"Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'Fixes in PL/SQL Deve'."
 
Generally v use Response.WriteFile or TransmitFile while downloading a file, The exception is here only..
 
Is there any property of update panel to be set to keep working with treeview or the update panel w'll not supported for treeview control?
 
And also any alternative for downloading the file from within updatepanel?
 
All the suggessions and solutions are appreciated and valueable to me.
 
Thands in advance.
Regards
Hasan.
QuestionWhat about a file upload control inside an ASCX user control inside an update panel Pinmemberfrankkirchner13:21 1 May '08  
AnswerRe: What about a file upload control inside an ASCX user control inside an update panel Pinmemberliammclennan13:41 1 May '08  
GeneralRe: What about a file upload control inside an ASCX user control inside an update panel Pinmemberfrankkirchner15:28 1 May '08  
AnswerSolution for using this technique with a user control Pinmemberkbomb9874:24 24 Jun '08  
GeneralRe: Solution for using this technique with a user control Pinmemberaikipoodle9:51 20 Jul '08  
GeneralRe: Solution for using this technique with a user control Pinmemberkaan0510:10 7 Aug '09  
GeneralRe: Solution for using this technique with a user control Pinmemberprincerc3:26 14 Feb '09  
QuestionIs it possible to call upload from a menu item? Pinmemberalan933:11 17 Mar '08  
AnswerRe: Is it possible to call upload from a menu item? Pinmemberliammclennan10:16 17 Mar '08  
GeneralRe: Is it possible to call upload from a menu item? Pinmemberalan936:34 18 Mar '08  
QuestionError when using postback trigger Pinmemberjeremygrand14:58 13 Dec '07  
GeneralThanks for the great tips PinmemberJ Navaneethan4:57 27 Nov '07  
GeneralThanks for the info Pinmemberaaron.barker13:57 9 Oct '07  
QuestionNot Shows the UpdateProgress [modified] Pinmembershan_y2005@yahoo.com20:51 30 Sep '07  
AnswerRe: Not Shows the UpdateProgress Pinmembersupernont8:48 1 Nov '07  
NewsMore reliable way Pinmemberbeep12:33 20 Aug '07  
GeneralRe: More reliable way Pinmembermkushner18:40 21 Aug '07  
Questiontrigger control inside formview edit mode Pinmembershadstrife19:19 20 Jun '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 30 Dec 2006
Article Copyright 2006 by liammclennan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid