Wednesday, October 12, 2005

Gmail file attachment secret! (input=file)

Ever since I started using gmail - One thing always bothered me is how they managed to hide the file control (input=file) and still be able to submit the form.

I have seen numerous solution i.e. Stick Blog has nice solution but it requires you to enable atleast one file control.

The problem is browser (IE) will not let you select the file programmatically (for security reasons) - The file has to be manually selected by user (Some people here claims that it use to work in IE.5.5 and Microsoft seems to have fixed this security hole in later version along with myriads of other security issues. Anyway the smart pants at google figured out a workaround this via use of <iframe&t; . So the trick is to invoke the click() from another window/frame.

Here is how it works

You have a one.htm file which contains your form and file elements

<form action="url" method = "post" enctype="multipart/form-data">

Then you have a file control in this form

<input id="file1" type=" file" name="file1">


Now in the second html page - you have a some code like this

<iframe name="frame1" src="one.htm" width="1" height="1">

and lets say you have anchor tag
<a onclick="selectFile()" href="#">Choose a file </a>

and you have a script like this to choose a file

<script>
function selectFile()
{
//first get the iframe
var frm1 = window.frames["frame1"]

//get the file control form iframe
var filecontrol = frm1.document.getElementById("file1")

//invoke the click() on the file control in iframe
file1.click();
}
</script>

You could also have the a submit button on this frame to trigger the submit in the iframe

<a href="#" onclick="dosubmit()" > Submit the form <a>

and the following doSubmit() will actually submit the form

<script>
function doSubmit()
{
//first get the iframe
var frm1 = window.frames["frame1"]

//get the form control form iframe
var form1 = frm1.document.forms[0]

//submit the form
form1.submit()

}

</script>

11 Comments:

At 10:54 AM, Jonathan said...

did you ever get this to work with multiple input fields? i'm using the solution here:
http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/

i'd love to incorporate your technique as well. thanks.

 
At 3:33 PM, raj said...

I also tried to execute this code but it seems not to be working.
I am also using the below code
http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element

I want to hide the browse button and textbox and replace it with an another button. I get this error "htmlfile:AccessDenied" at this line
document.forms["frmUpload"].submit();

Any help will be appreciated.

 
At 11:57 AM, Bruce Dumes said...

There is a bug in the example.


//get the file control form iframe
var filecontrol = frm1.document.getElementById("file1")

//invoke the click() on the file control in iframe
file1.click();


The last line should be:

filecontrol.click();

Bruce

 
At 12:42 PM, Sheetal Jain said...

Thanks for the correction bruce

 
At 12:43 PM, Sheetal Jain said...

Yes I did get it to work with multiple fields, I had to put unique filecontrol in both places and then access it viw getElementById()

 
At 5:29 AM, Er Pei said...

¡Works perfectly!

¡Thank you very much!

 
At 2:40 AM, Jonathan said...

another question... if the form is in an iframe, how can you tell if the form has submitted? i need the page to perform an action when the submit is complete, is there a way? thanks.

 
At 7:58 PM, Sheetal Jain said...

Jonathan

You can use 'readystate' window property to see if the iframe is reloaded.

 
At 7:06 AM, Koen Deforche said...

The click() trick does not work on Firefox or Opera, two browsers which GMail does handle well.

There must be something more to it ?

 
At 9:43 PM, Great Future said...

Unlimited Earnings Potential - http://1greatfuture.com

Our company is rapidly growing and offers you an extraordinary income helping others succeed. The primary requirement is to follow up on client inquiries and point them in the right direction. It is stress free, rewarding and straightforward work.

For complete details: http://1greatfuture.com


(Please feel free to delete this post if you don't want it on your blog. Thanks for the informative blog and opportunity to post.)

 
At 2:57 PM, Free Webmail Program said...

Webmail program for the major free email sites -
http://inovasphere.com/mmail.htm

My Mail 1.0 is configured to work with AOL, Gmail, Hotmail, Linuxmail, and Yahoo. With My Mail 1.0 you get the benefit of premium services without having to pay site fees. My Mail 1.0 completely automates the process of sending and receiving mail from the major sites, saving you time and trouble.

My Mail 1.0 eliminates the need to visit web sites to send and receive mail, which increases the speed of sending and receiving email by over 80%, even if they do not offer what is known as POP3, IMAP and SMTP. My Mail 1.0's look is also fully customizable. One you use it, you'll never want to go back to the web site again to get your mail.

For complete details: http://inovasphere.com/mmail.htm




(Please feel free to delete this post if you don't want it on your blog. Thanks for the informative blog and opportunity to post.)

 

Post a Comment

<< Home