Click here to Skip to main content
Click here to Skip to main content

Customize AsyncFileUpload (Microsoft Ajax control toolkit), Cross Browser too

By , 21 Feb 2011
 

Introduction

This article is about how to customize the look and feel of AsyncFileUpload from Microsoft Ajax control toolkit. The source code for both VS 2010 and VS 2005 is available. A live demo is also available for you to see how it runs.

Background

Ajax control toolkit is a Microsoft open source project. Among the toolkit, there is a very useful control called “AsyncFileUpload”. You can see Microsoft's official demo of this control here.

It's powerful but hard to customize from its built-in properties.

I successfully customized it through two approaches. You can see my live demo here.

Using the Code

After you extract the source code, you can directly open the solution file (one for VS2010, the other for VS2005 just in case).

To keep it simple, they are both edited under "web site" project not "web application". You can directly run them by clicking "run" from IDE.

NOTE: VS2010 project needs .NET 4.0 installed locally for debugging. VS2005 project needs .NET 3.5 installed locally for debugging.

Points of Interest

The basic idea of customizing AsyncFileUploader is using mask trick:

You make the AsyncFileUploader transparent and have a button or image looks covered AsyncFileUploader. But actually, the AsyncFileUploader is on top of masked button/image. So the user seems to click the masked customized button/image, but actually clicks the AsyncFileUploader.

That's just basic idea. I implement it in two approaches.

Approach 1 is easy and quick. Basically, you make a customized button with similar size of AsyncFileUploader. The limitation of this approach is you cannot make big button because the size of AsyncFileUploader itself is hard to adjust (especially the height if it's even possible).

filup_approach1.png

Approach 2 allows me to be proud for myself: The idea is that you cannot adjust AsyncFileUploader size easily, but you can move it. So I move it inside of the big image button. Whenever user clicks, the AsyncFileUploader gets clicked.

To make it more clear, I have a X/Y position box to show you the mouse position when it's in the image scope. If you really want to see how the internal control is moving, you can change the transparent value of the control (it's 0 to be totally transparent, you can change it to 50 as half transparent).

filup_approach2.png

Things to be Aware of

I didn’t find a good way to gracefully catch the over size uploading issue. I tried global.asax application error catching, also tried AsyncFileUpload built-in error catching. It seems global.asax can catch the specific error, but cannot throw it gracefully. So I end up using warning UI and <httpRuntime maxRequestLength="500"/> from web.config. It’s not perfect, but works for all major browsers.

Another thing to be aware of: The solution also works under latest IE9 RC, but you have to run it under IE8 compatible mode.

SUMMARY

All codes are tested under major browsers: IE8/IE9 RC/Firefox 3.6/Chrome 9. It's nice to have such a built-in control to work with.

Hope this is helpful in your projects as well.

License

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

About the Author

Simon Pang
Software Developer
Canada Canada
Member
a believer of "One app worths thousand words" and a business application developer in Toronto Canada.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberRazi Syed14 May '12 - 6:42 
Awesome! Helped me a lot after wasting a couple of hours on other solutions. Thanks!!!
GeneralMy vote of 4memberSteve Krile8 Dec '11 - 14:35 
Got me started on my way. However, in the end I took a different approach. Used your styling on the control to hide it then put this line of jquery in an image tag next to the control: onclick="$(this).parent().find('input[type=file]').trigger('click')"
 
This simulates clicking on the file control.
QuestionSolution does not work in IE9memberlat23311 Oct '11 - 22:15 
Thanks for your excellent solution. However it does not work on IE9. I try with IE 9.0.8112.16421 and when I click on the "Upload file" button, nothing happens, with both 2 approaches.
 
Could you help me to fix that?
thanks
AnswerRe: Solution does not work in IE9memberSimon Pang12 Oct '11 - 21:03 
While this was posted, I tested it with IE9.0 RC.
 
You're right, it doesn't work with IE 9.0 final release by default.
But there is a work around:
You can click the little "broken page" button beside the address bar of IE9, which is IE8 compatiblity button. Once this is clicked, The both demos worked. I tested it.
 
As for final fix for IE9 standard mode, I don't have time now. but I'll look into it.
 
Thanks.
GeneralRe: Solution does not work in IE9memberlat23313 Oct '11 - 16:31 
Hi
I've found a IE9 bug fixed version of Ajax Control Toolkit and when replace it into your code, the page runs properly in IE9. In conclusion: It's a bug of Ajax control toolkit, not yours Big Grin | :-D
 
Try it at: http://ajaxcontroltoolkit.codeplex.com/releases/view/71000
 
Thanks again for your great solution.
 
Tuan
Questionhow to bind a gridview after the file uploads? and how to validate a file clinetside?memberVaibhav Saran16 Mar '11 - 2:09 
Hi,
could anybody describe how to validate a file before it uploads?
And also i'm looking for the how to binfd gridview after the upload completes?
QuestionAny way to get it to work n a JQuery modal dialog?memberfred_1 Mar '11 - 2:54 
For some reason it won't work there

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 21 Feb 2011
Article Copyright 2011 by Simon Pang
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid