Click here to Skip to main content
Licence CPOL
First Posted 4 Mar 2007
Views 104,301
Downloads 1,811
Bookmarked 93 times

Gmail like file upload button

By | 4 Mar 2007 | Article
An inherited button which helps to upload files using the hidden IFRAME technique
Screenshot - GmailUpload.gif

Introduction

This is an inherited button control which can perform file upload like in gmail manner. The button uses the inherited IFRAME technique to avoid full page rendering so that user will get a nice experience.

Background

I have searched for an article to perform GMail like file upload. It seems that we cannot perform file uploading using AJAX. I got some nice javascript from webtoolkit which was written to operate with PHP. The script uses the hidden IFRAME technique. Here I have created an derived ASP.NET Button control which can perform the trick.

Using the code

To open the project, unzip it and open the file FileUploadControlTest.sln Open the file UploadButton.cs. The OnInit method will register the LoadComplete event, include the javascript which is a web resource (for more information on webresource click here), and sends the onsubmit statement to client.

    /// <summary />
    /// Basic registration of events
    /// </summary />
    /// <param name="e" /></param />
    protected override void OnInit(EventArgs e)
    {
        this.Page.LoadComplete += new EventHandler(Page_LoadComplete);
        base.OnInit(e);
        this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), 
            "ScriptBlock", 
            this.Page.ClientScript.GetWebResourceUrl(this.GetType(), 
            "WebControls.JScripts.AIMScript.js"));
        string onsubmitstatement = "return AIM.submit(document.forms[0], 
            {'onStart' : " + OnStartScript + ", 
            'onComplete' : " + OnCompleteScript + "})";
        this.Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), 
            "OnSubmitScript", onsubmitstatement);
    }

The onsubmit script creates a new hidden IFRAME in the dom and sets the target of the form to this IFRAME. So the section the user is seeing will not get redrawn after postback. In the Page_LoadComplete event of the page, we will get the FileUpload control filled with the uploaded file. We will call the event handler in the Page_LoadComplete method. The return text after executing the event handler will be returned to the client.

    /// <summary />
    /// After completing page load, call the event handler on page to perform
    /// operations on uploaded file
    /// </summary />
    /// <param name="sender" /></param />
    /// <param name="e" /></param />
    void Page_LoadComplete(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (this.Parent.FindControl(RelatedFileUploadControlId) != null)
            {
                FileUpload fu = 
        this.Parent.FindControl(RelatedFileUploadControlId) as FileUpload;
                UploadButtonEventArgs args = new UploadButtonEventArgs(fu);
                UploadButtonEventHandler handler = 
                      (UploadButtonEventHandler)Events[EventUploadClickKey];
                if (handler != null)
                {
                    try
                    {
                        WriteTextToClient(handler(this, args));
                    }
                    catch (System.Threading.ThreadAbortException ex)
                    {
                        // do nothing
                    }
                    catch (Exception ex)
                    {
                        WriteTextToClient("An exception occurred - " + 
                                          ex.Message);
                    }
                }
                else
                {
                    WriteTextToClient("Handler not registered");
                }
            }
        }
    }

In the onload method of IFRAME, we will call the function registered as OnCompleteScript, and pass the text inside the IFRAME which is the response text.

Points of Interest

The hidden IFRAME technique is an old one but in some complex situations, it will help us !!!

License

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

About the Author

Shyam S

Architect

India India

Member

I am a software architect in a reputed software firm. Mainly I am dealing with presentation layer like creation of re-usable framework, controls, pages etc.

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
QuestionAJAX Script Manager conflicting with the Custom Fileupload Control Pinmemberqingliu614:26 28 Jan '10  
AnswerRe: AJAX Script Manager conflicting with the Custom Fileupload Control PinmemberShyam S19:23 28 Jan '10  
GeneralGmail like file upload PinmemberShyam S22:00 23 Jun '09  
Generalwhen I am working in MasterPage its not wrking PinmemberMember 420977020:33 12 Apr '09  
QuestionHow can I work with this file upload control and .net validation controls? Pinmemberkaivanshah19:38 19 Jan '09  
Generalcomplements PinmemberMember 37673482:27 20 Oct '08  
GeneralNice article, but... Pinmemberdennieku23:15 9 Oct '08  
GeneralRe: Nice article, but... Pinmemberdennieku23:16 9 Oct '08  
GeneralThe uploaded file location Pinmemberarnonzamir14:30 3 Sep '08  
GeneralRe: The uploaded file location Pinmemberctembe0:18 11 Oct '08  
QuestionProblem with postback PinmemberMember 3595177:45 29 May '08  
AnswerRe: Problem with postback PinmemberShyam S18:18 29 May '08  
GeneralRe: Problem with postback PinmemberMember 42097703:10 13 Apr '09  
GeneralDynamically use file upload control Pinmembershahgun23:06 27 Jun '07  
GeneralDOTNET 2 PinmemberRichardOfMercator0:59 31 May '07  
Hi ShyamProxy,
 
Can I use your control to surpas the upload limit that I would normally see when using upload controls?
 
Is this Ajax upload button for DoTNet2 by the way and when is the Java edition coming out? I think we can really use it!!
 

Cheers,
 
RichardOfMercator
GeneralMissing the FileUploadControlTest Project Pinmemberclivenyong23:22 11 Apr '07  
GeneralDon't work with UpdatePanel Pinmemberdkc_project22:08 5 Mar '07  
GeneralRe: Don't work with UpdatePanel Pinmembershyamproxy22:48 5 Mar '07  
GeneralRe: Don't work with UpdatePanel Pinmemberyaniar15:09 28 Mar '07  
GeneralNice article....Thanks to shyamproxy PinmemberNajeed19:30 5 Mar '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
Web03 | 2.5.120529.1 | Last Updated 4 Mar 2007
Article Copyright 2007 by Shyam S
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid