I am having trouble trying to allow the user to select an image file and display on screen using asp.net. i am using a file upload control (<input id="fileUpload" type="file") however i do not want to use the text box and browse button that come with the file upload control so i have hidden it. in order to show the open file dialog box i have a button (<input id="btnBrowseProfilePicture" type="button" value="Browse" />) which has the code 'fileUpload.click()' in it's onclick event. I then have code 'hdfFilePath.Value = this.Value;' in the onchange event of the file upload control which sets the value of an asp:Hidden field to the path of the file the user selected. the hidden field has a function from the c# code behind in the onvaluechanged event however this never seems to fire. i have tried refreshing the page or forcing a post back after setting the value of the hidden field but it never fires. could anyone tell me what i am doing wrong or suggest a better way of displaying an image in this fashion. please not that if i can get pass file path from the file upload control into a c# function in the code behind then i know how to display the image.
ASP Code
C# CodeCode:<asp:HiddenField ID="hdfFilePath" runat="server" onvaluechanged="hdfFilePath_ValueChanged" /> <input id="fileUpload" type="file" style="display: none" onchange="ctl00$cphContent$hdfFilePath.Value = this.Value;" /> <input id="btnBrowseProfilePicture" type="button" value="Browse" onclick="fileUpload.click(); __doPostBack('hdfFilePath','ctl00$cphContent$hdfFilePath.Value');" name="btnBrowse" runat="server" />
Code:protected void hdfFilePath_ValueChanged(object sender, EventArgs e) { string strPath = hdfFilePath.Value; }


Reply With Quote