FileUpload - listening for file browse, is this possible?

Last post 07-26-2007 7:28 PM by d4dennis@inspir3. 3 replies.

Sort Posts:

  • FileUpload - listening for file browse, is this possible?

    07-26-2007, 1:58 PM
    • Loading...
    • anyeone
    • Joined on 11-08-2004, 11:13 AM
    • Posts 21

    Hi,

    What I'm trying to do is this:   I have a FileUpload control for the user to select the file from their local machine to upload. I also have a separate "name" textbox for the user to enter a logical name for the file.  What I want is, after the user browses for a file, to take the actual physical name of the file and populate the name textbox with that.  Reason being, then they can optionally update the logical name without affecting the physical name, but clearly show that the physical filename is the default. (And also give the option to change the logical name of the file later without requiring an upload).

    The problem is, the FileUpload control is really light on events.  I don't see any event to listen for that corresponds with what I would call an "OnBrowseComplete" where I could then simply set txtName.Text = FileUpload1.FileName. No idea why the control designers didn't give us such a useful event but that's another question.

    I am open to a javascript solution but again am not sure what event would be listened for on the client side and also I am not sure how to get the filename property out of the FileUpload control in Javascript, or if that is even possible.

    Any clever solutions from the peanut gallery?

     

    Cheers,

    Anye 

  • Re: FileUpload - listening for file browse, is this possible?

    07-26-2007, 6:47 PM
    Answer

    Hi There,

    Above is an simple example to set event on fileupload control. hope it help

    <head runat="server">

    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">

    function FileUpload_OnChange(sender,e)

    {

    document.getElementById(
    'TextBox1').value = sender.value;

    }

    </script>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:FileUpload ID="FileUpload1" runat="server" onchange="FileUpload_OnChange(this,event);" />

    \

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>

    </form> </body>

    </html>

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
  • Re: FileUpload - listening for file browse, is this possible?

    07-26-2007, 7:19 PM
    • Loading...
    • anyeone
    • Joined on 11-08-2004, 11:13 AM
    • Posts 21

    Thanks, this worked, I just enhanced it a bit by adding a txtid argument to the method and passing the textbox's ClientID.

    I didn't realize the FileUpload control had a "value" property, is there some generic way for determining what property on a webcontrol will map to the javascript value? 

  • Re: FileUpload - listening for file browse, is this possible?

    07-26-2007, 7:28 PM

    Hi anyeone,

    When page render, most server control is render to html.

    Hence, <ASP:FileUpload is render to <input type="file"

    So, an input will retain all its property.

    For more information http://www.w3schools.com/htmldom/dom_obj_fileupload.asp

     

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter