Yesterday I blogged on the way to start a fileopen dialog from a web-page. Thanks to the feedback we discovered there's a very bad turn to that solution, the ActiveX control used only works on a machine which has MS devtools installed. Which is your or my machine but not that of the customer who is going to use the application. On the web I found a new solution which is pretty neat and doesn't need any ActiveX at all.
In HTML there's the INPUT control of type file. It is an input box which automatically gets a button. Clicking this button will start the desired file-open dialog, completing that copies the filename into the textbox. Using the example found on the web I have crafted the following solution.
- Create a hidden input file control
- Create a script function which fires the click of the file-input control and copies the selected value into an asp.net textbox
- Add an onclick handler to a button to fire this script function.
This is the script snippet on the page
<SCRIPT language=JavaScript> function BrowseClick(){
document.forms[0]['fileBrowse'].click();
document.forms[0]['TextBox1'].value = document.forms[0]['fileBrowse'].value;
return false;}
</SCRIPT>
Which is a stripped version of the example found. Again I assemble the script from code
const string fiName = "fileBrowse";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<div style='visibility: hidden; width: 0px; height: 0px'>");
sb.Append(string.Format("<input type=file id='{0}'></div>", fiName));
sb.Append("<Script language=JavaScript> function BrowseClick(){");
sb.Append(String.Format("document.forms[0]['{0}'].click();", fiName));
sb.Append(String.Format("document.forms[0]['{0}'].value = document.forms[0]['{1}'].value;", TextBox1.ClientID, fiName));
sb.Append("return false;} </Script>");
RegisterClientScriptBlock(scriptKey, sb.ToString());
ButtonLinkNaarDocumentatie.Attributes.Add("OnClick", "return BrowseClick();");
This also works on my wife's XP-home machine. Thanks a lot to Andrew, Micheal Harris and Botolph.
blog on,
Peter
Posted
Wed, Dec 3 2003 5:54 PM
by
pvanooijen