5,783,659 members and growing! (14,012 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » Controls     Intermediate License: The Code Project Open License (CPOL)

Gmail like file upload button

By Shyam S

An inherited button which helps to upload files using the hidden IFRAME technique
Javascript, XML, HTML, C# 2.0, C#, Windows, .NET, .NET 2.0, ASP.NET, WebForms, Ajax, VS2005, Visual Studio, Dev

Posted: 4 Mar 2007
Updated: 4 Mar 2007
Views: 46,844
Bookmarked: 70 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 3.60 Rating: 3.06 out of 5
4 votes, 28.6%
1
1 vote, 7.1%
2
3 votes, 21.4%
3
1 vote, 7.1%
4
5 votes, 35.7%
5
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 />

    /// 

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

    /// 

    /// 

    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


I am a software architect in Proxy Systems, Technopark, Trivandrum, India. Mainly I am dealing with presentation layer like creation of re-usable framework, controls, pages etc.
Occupation: Web Developer
Location: India India

Other popular Ajax and Atlas articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralcomplementsmemberMember 37673483:27 20 Oct '08  
GeneralNice article, but...memberdennieku0:15 10 Oct '08  
GeneralRe: Nice article, but...memberdennieku0:16 10 Oct '08  
GeneralThe uploaded file locationmemberarnonzamir15:30 3 Sep '08  
GeneralRe: The uploaded file locationmemberctembe1:18 11 Oct '08  
QuestionProblem with postbackmemberMember 3595178:45 29 May '08  
AnswerRe: Problem with postbackmemberShyam S19:18 29 May '08  
GeneralDynamically use file upload controlmembershahgun0:06 28 Jun '07  
GeneralDOTNET 2memberRichardOfMercator1:59 31 May '07  
GeneralMissing the FileUploadControlTest Projectmemberclivenyong0:22 12 Apr '07  
GeneralDon't work with UpdatePanelmemberdkc_project23:08 5 Mar '07  
GeneralRe: Don't work with UpdatePanelmembershyamproxy23:48 5 Mar '07  
GeneralRe: Don't work with UpdatePanelmemberyaniar16:09 28 Mar '07  
GeneralNice article....Thanks to shyamproxymemberNajeed20:30 5 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Mar 2007
Editor: Chris Maunder
Copyright 2007 by Shyam S
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project