Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have used a File upload control in my project.
Now, i want to display Filename selected in Fileupload control in a textbox as soon as user selects a file.
Need quick reply.
Posted
Comments
Marrinavincent 2-Nov-12 1:25am    
I am using fileupload control inside the update panel.. It works fine.. But when i try to use the jquery validation for the controls. Button click not work.. By code is below.. please help me

<asp:UpdatePanel ID="updatecompanydetails" runat="server">
<contenttemplate>
<script type="text/javascript">

Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckValidationStatus);
</script>
<div class='validate form-horizontal'>
<table cellpadding="3" cellspacing="3">
<tr>
<td>
<div class="control-group">
<label for="uname1" class="control-label">
Company Name</label>
<div class="controls">
<asp:TextBox ID="txtcompanyname" runat="server" name="uname1" class='required'>
<asp:TextBox ID="TextBox1" runat="server" >
</div>
</div>
</td>
<td rowspan="3">
<asp:Image ID="imglogo" runat="server" />
</td>
</tr>
<tr>
<td>
<div class="control-group">
<label for="email1" class="control-label">
Website</label>
<div class="controls">
<asp:TextBox ID="txtwebsite" runat="server" class='url'>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="control-group">
<label for="file2" class="control-label">
Logo</label>
<div class="controls">
<asp:FileUpload ID="filelogo" runat="server" onchange="callme(this)">
</div>
</div>
</td>
</tr>
</table>
<div class="form-actions">
<asp:Button ID="btnsubmit" runat="server" Text="Submit" CssClass="navigation_button btn-primary"
OnClick="btnsubmit_Click" />
<asp:Button ID="btnreset" runat="server" Text="Reset" CssClass="btn btn-danger"
OnClick="btnreset_Click" />

</div>
</div>

ok the file upload control has a property called FileName you can retrieve the name of a selceted file
however ,there is another proerty called postedFile this will retrieve an object of type httpPostFile object then it has a mazing porperties as

FileUpload obj;//aleady exist in the web form
//send your page back to server
C#
if(mypage.IspostedBack)
{
if(obj.hasFile)
{httppostedFile obj2=obj.postedFile;
obj2.fileName
obj2.contentType
}
}
and so on..
thank u

[Edit]
Ankur: The code you have given, needs a postback.
[/Edit]
 
Share this answer
 
v2
you can use javascript to show selected file by upload controle

ASP.NET
<div class="controls">
<asp:fileupload id="filelogo" runat="server" onchange="callme()" xmlns:asp="#unknown">
</asp:fileupload></div>

add textbox here
XML
<pre lang="HTML"><div class="controls">
<asp:FileUpload ID="filelogo" runat="server" onchange="callme(this)">
</div></pre>
<asp:textbox id="txtUploadedFileName" runat="server" xmlns:asp="#unknown"></asp:textbox>


now write the function on .aspx file

HTML
<script type="text/javascript">
function callme()
  {
   document.getElementById('<%=txtUploadedFileName.ClientID%>').value=document.getElementById('<%=filelogo.ClientID %>').value;
            
  }

</script></script>


once you will select any file in uploadcontrol name of that file will reflect in text box.
 
Share this answer
 
C#
function callme(oFile) {
         document.getElementById("TextBox1").value= oFile.value;
       }




XML
<asp:FileUpload ID="FileUpload1" runat="server"  onchange="callme(this)" />
       <asp:TextBox ID="TextBox1" runat="server"   ></asp:TextBox>


[Edit]
Ankur: But that would give you the full path.
You can easily extract the file name from that using javascript.
[/Edit]
Happy codding :)
 
Share this answer
 
v2
Comments
Marrinavincent 2-Nov-12 0:50am    
How to solve?
I am using jquery for validating the control. Button click always shows the filename is empty.. the above code not working for fileupload control.. please help me. (All the control inside updatepanel)

Thank you
dsanthosh 26-Mar-15 5:06am    
I am using your code. It give path like "C:\fakepath\1.jpg" insteed of "C:\COR\file\myimage\i.jpg". How to get actual path?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900