using the HtmlInputFile

N

nes

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
 
J

Josh Twist

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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top