using the HtmlInputFile

Discussion in 'ASP .Net' started by nes, Jan 12, 2006.

  1. nes

    nes Guest

    Hi all,

    For uploading files i am using the HtmlInputFile. Now I want to do
    things with it. First I want to give the button a specific style an
    second when a postback happens I don't want the selected file t
    disappear from the HtmlInputFile.
    Now on the web I found the next thing:

    <html>
    <head>
    <title>File Upload Example</title>
    <script language="JavaScript" type="text/javascript">
    function HandleFileButtonClick()
    {
    document.frmUpload.myFile.click();
    document.frmUpload.txtFakeText.value
    document.frmUpload.myFile.value;
    }
    </script>
    </head>
    <body>
    <form name="frmUpload">
    <!-- Real Input field, but hidden-->
    <input type="file" name="myFile" style="display: none">
    <!-- Fake field to fool the user -->
    <input type="text" name="txtFakeText" readonly="true">
    <!-- Button to invoke the click of the File Input -->
    <input type="button" onclick="HandleFileButtonClick();
    value="Upload File" style="background: red;">
    </form>
    </body>
    </html>

    Now this works really perfect. You don't really use the htmlInputfil
    but separate button and textbox so you can give your style and you hav
    the file name showing.

    My problem is that I have an ASP:BUTTON for Submitting my file (som
    reading, changing, ...) But when you click the button for the firs
    time it erases the value from the HtmlInputFile. So you have to pres
    again for really doing your submit code.
    You don't have that problem when you are using a HtmlInputButton(if yo
    use clientscript), but unfortunately I can't use it.
    Has anyone got a suggestion how I can resolve the problem of have t
    press the button twice?

    Thx

    Ne

    --
    ne
    -----------------------------------------------------------------------
    nes's Profile: http://www.highdots.com/forums/m172
    View this thread: http://www.highdots.com/forums/t319845
    nes, Jan 12, 2006
    #1
    1. Advertising

  2. nes

    Josh Twist Guest

    I've struggled with this one myself in the past. Unfortunately, I found
    that the best thing to do was to leave the file input alone and just
    let it be :|

    It seems the clearing of the input type=file (ITF from now on) when the
    form is submitted is an IE security feature. The only way to avoid this
    is to ACTUALLY click on the ITF's browse button or enter the path
    directly. It has nothing to do with ASP.NET - you can recreate it in
    plain old html.

    Furthermore, the script doesn't work at all in Firefox et al.

    Finally, the user wouldn't be able to re-upload the file a second time
    just because you kept the value in txtFakeText. You'd need to put that
    value back into the ITF when the page reloaded, and as I'm sure you're
    aware, that's just not possible (and with good security reasons!)

    Let me know if you find an amazing solution though!

    HTH

    Josh
    http://www.thejoyofcode.com/
    Josh Twist, Jan 12, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Viktor Popov

    using HtmlInputFile problem

    Viktor Popov, Aug 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,606
    Alan Ferrandiz [MCT]
    Aug 7, 2004
  2. Long Le
    Replies:
    3
    Views:
    1,167
    Long Le
    Aug 11, 2004
  3. Asela Gunawardena
    Replies:
    1
    Views:
    2,361
    Nathan Sokalski
    Dec 21, 2005
  4. Chris
    Replies:
    4
    Views:
    589
    Chris
    Feb 2, 2007
  5. Vaquar
    Replies:
    3
    Views:
    507
    Coskun SUNALI [MVP]
    Feb 5, 2008
Loading...

Share This Page