How to open file dialog in ASP.NET?
We need two controls, one TextBox named txtFile and one Button named btnBrowse.
Add the following code in Page_Load
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}' accept='image/gif,image/jpeg'></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;", txtFile.ClientID, fiName));
sb.Append("return false;} </Script>");
RegisterClientScriptBlock("scriptKey", sb.ToString());
btnBrowse.Attributes.Add("OnClick", "return BrowseClick();");
We need two controls, one TextBox named txtFile and one Button named btnBrowse.
Add the following code in Page_Load
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}' accept='image/gif,image/jpeg'></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;", txtFile.ClientID, fiName));
sb.Append("return false;} </Script>");
RegisterClientScriptBlock("scriptKey", sb.ToString());
btnBrowse.Attributes.Add("OnClick", "return BrowseClick();");
Comments
Post a Comment