FileUpload in UpdatePanel, ASP.NET, like Gmail

Earlier, I had written this post on how you can accomplish using FileUpload control in UpdatePanel since by default it wasn’t supported.  The post seems to have helped many and keeps being the most visited one.  However, with the new release of Ajax Control Toolkit (v 3.0.30930) released specifically for .NET 3.5 SP1 (with Visual Studio 2008 SP1), there are couple of new controls.  One of them is the AsyncFileUpload control.

Thanks to the codeplex community which keeps getting better and better with time, the Ajax Control Toolkit has grown into one of our largest community contributed controls for ASP.NET with about 43 controls that help in accomplishing rich user experiences in ASP.NET Websites.

The AsyncFileUpload is one simple way of accomplishing what I had written earlier using PostbackTrigger, the regular FileUpload control etc.,  To be able to use the AsyncFileUpload Control, you must have the latest version of AjaxControlToolkit installed.  The other pre-requisites are obviously NET 3.5 SP1 and Visual Studio 2008 SP1 (or the free Visual Web Developer Express Edition)

You can download the pre-requisites from the respective links above.  For downloading the AjaxControlToolkit, visit the CodePlex site.  You can download just the binary files or the Source files as well, if you require to modify.  The Script Files is useful if you want to just work with the client side scripts and not use the server controls.

Once you have downloaded, you would need to add them to Visual Studio or VWD.

1. Open Visual Studio and create a new webapplication or website.  Click to open the ToolBox

2. Right Click and select “Add Tab”

3. Provide a name say “Ajax Control Toolkit”

4. Right Click the newly created tab and select “Choose Items”

5. Click on the “Browse” button in the file dialog that opens and browse to the place where you downloaded the AjaxControlToolkit binaries

6. Typically I would put them under C:\Program Files\Microsoft ASP.NET for consistency.

7. Select the AjaxControlToolkit.dll and it would list all the new controls.

8. Click “Ok” to add all the controls.

9. You should now see under the newly created toolbox tab these controls.

Once you are done with above, create a simple Default.aspx page in the application you created and drop the Script Manager control into your webform.  Next add an UpdatePanel with ContentTemplate.  Inside the ContentTemplate, add the AsyncFileUpload control into the webform as well as a button and 2 labels for the uploading and displaying messages respectively.  The markup looks something like below

<form id="form1" runat="server">
   <div>
       <asp:ScriptManager ID="ScriptManager1" runat="server">
       </asp:ScriptManager>
       <asp:Image ID="img1" runat="server" ImageUrl="~/Images/spin2.png" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
               <cc1:AsyncFileUpload ID="AsyncFileUpload1" runat="server" UploaderStyle="Modern" ThrobberID="img1"  />
       <br />
       <asp:Button ID="btnUpload" runat="server" Text="Upload"
           onclick="btnUpload_Click"   />
           <br />
           <asp:Label ID="Label1" runat="server" /> 
           <br />
           <br />
         </ContentTemplate>
       </asp:UpdatePanel>
       <br />
       <asp:Label ID="Label2" runat="server" />
   </div>
   </form>

Also, you can see that I have added an asp:Image pointing to a spin image that is specified as the ID for ThrobberID in the AsyncFileUpload definition.  This is optional but nice to have since this would display the throbber icon while uploading takes time.

Once you are done, you would need to define the action in the codebehind or in the script

protected void Page_Load(object sender, EventArgs e)
      {
          Label2.Text = DateTime.Now.ToString();
      }

      protected void btnUpload_Click(object sender, EventArgs e)
      {
          AsyncFileUpload1.SaveAs(Server.MapPath((AsyncFileUpload1.FileName)));
          Label1.Text = "You uploaded " + AsyncFileUpload1.FileName;
      }

Notice, the Label in the Page_Load event is just to indicate that indeed the operation happened asynchronously since the time that is displayed initially doesn’t change once you click on Upload button. 

Try running this and you will find that the whole operation happens asynchronously without a full page reload.  Note that, you would need to still put the AsyncFileUpload control inside UpdatPanel for this behaviour.  Otherwise, it would behave like a regular postback control.

You can download the sample code from

Cheers !!

posted @ Thursday, October 01, 2009 5:31 AM

Print

Comments on this entry:

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by setc at 10/1/2009 7:11 AM
Gravatar
Much more easier. Finally i felt like we are on a simple but robust path for file uploads. Happen to hit on this post faster, thanks for posting.

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by k5 at 10/1/2009 8:53 AM
Gravatar
would this work with large files?

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by Lee at 10/1/2009 9:16 AM
Gravatar
Actually the standard file upload control works in an update panel with a postbacktrigger. The article English is misleading.

# wow. it's cool

Left by Ian lee at 10/1/2009 2:14 PM
Gravatar
it's what i am looking for. thanks so much~

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by John Sinnott at 10/2/2009 1:19 AM
Gravatar
How large a file will this handle?

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by D Field at 10/2/2009 2:41 PM
Gravatar

It's good to see some progress being made on a more useful file uploader control. It works fine in IE8, but does not work properly in firefox 3.53: the throbber item is always visible. Any clues about a workaround for this?

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by D Field at 10/3/2009 2:13 AM
Gravatar

My bad--turns out that I had disable javascript. Never mind!

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by jeff at 10/5/2009 9:40 PM
Gravatar
Is it works with large file? Let download it :)

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by Santiago at 10/6/2009 2:26 PM
Gravatar
could not make it work with large files, I guess the problem is the persisted storage used (Session)

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by bass fish at 10/13/2009 8:20 AM
Gravatar
I can not get it to work when the asyncfileupload control is on a custom control - any suggestions?

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by Gopi T at 10/13/2009 9:57 PM
Gravatar
Oh Great....

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by Tim at 10/17/2009 7:51 PM
Gravatar
Santiago - To enable large files to be uloaded you must modify the web.config..... 4Mb is default but you can change this

# re: FileUpload in UpdatePanel, ASP.NET, like Gmail

Left by Erik Larsson at 11/16/2009 12:52 AM
Gravatar
How does the AsyncFileUpload connect to the button?
Also AsyncFileUpload starts uploading as soon as the file has been choosen. By stating the onUploadedComplete (which is stated UploadedComplete in the sample-page which is quite strange...) to a specific backend function (as a event that is) it should be triggered that way.

Problem with AsyncFileUpload is that it doesn't trigger the specified event. It just seems damn broken... I am using Ajax Toolkit version 3.0.30930.28736

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345