Click here to Skip to main content
14,302,925 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hallo,

I have a File Upload and a Textbox field on my asp-page. Now I would like to create a button, that copies the name of the selected file into my textbox using javascript. How can I do that?

<asp:FileUpload ID="f_file" runat="server" />
<asp:TextBox ID="f_text" runat="server"></asp:TextBox>
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

call following function on onclientclick of button
function setPath()
{     
    var objReg = new RegExp(/([^\/\\]+)$/);
    var strName = objReg.exec(document.getelementById("f_file").value);
    document.getelementById("f_text").value = strName[0];   
}
   
v2
Comments
chris085 8-Nov-11 10:25am
   
I tried it and after clicking the button the selected file "disappears" from the FileUpload Element. I suppose, because the page is loaded again. Is there any chance to prevent this from happening?
Rate this:
Please Sign up or sign in to vote.

Solution 2

$(function() {
    $("#button1").click(function()
    {
        $("#tb1").val($("#fs1").val());
        return false;
    });
});


For security reason probably the name of the path in my chrome is always "C:\fakepath\".
The name of the file is right though.
sample[^]
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100