views:

343

answers:

2

My testers have discovered that if you type free text into a file upload input then none of the buttons on the page work until that text is removed (so the page cannot be submitted).

I am able to replicate this with the following ASPX code (with no code behind):

<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <body>
    <form id="form1" runat="server">
      <div>
        <asp:FileUpload ID="fuTest" runat="server" />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
      </div>
    </form>
  </body>
</html>

(Note that I haven't bound any handlers to the page; despite this, the page is submitted when the submit button is clicked only if no text is entered into the upload text box)

Is there any way to prevent users from typing free text into a file upload control? It seems that this is only possible in IE - Firefox and Chrome natively prevent text from being entered into upload input fields.

I've seen solutions elsewhere which suggest hiding input and replacing it with a label / button combo, but this seems like it might cause more problems and work inconsistently across browsers.

Any thoughts?

+1  A: 

I'm not sure if this would work as expected, but have you tried: <input readonly="readonly">

Jeremy
That disables input completely in IE - the "Browse" button no longer works (although it doesn't appear disabled, it just do nothing). Interestingly, adding readonly="readonly" does nothing in Firefox or Chrome
Dexter
Can you add a keypress event that negates any input from the keyboard? Again, I'm not sure if this would work with the file input element-- just throwing out ideas.
Jeremy
Handling the keypress does work, thanks @Jer. I've included my solution as an answer here.
Dexter
Ironically I had to take my own advice today to fix an issue at work :-P
Jeremy
+1  A: 

As per @Jer's suggestion, I was able to prevent input into the file upload without breaking any of the other functionality by handling keypress events on the upload. Using jQuery:

$(document).ready() {
    $('input:file').keypress(function() {
      return false;
    });
}
Dexter