views:

314

answers:

2

This code should work in IE, but it doesn't. (Please don't even test it in Firefox, because I won't use this approach in it.) I get the open dialogue to be triggered, but what I want now is to display the name of the attached file inside a span whenever the user makes his/her selection. Any help?

<html>
<head>
  <title>example</title>    
  <script type="text/javascript" src="../js/jquery.js"></script>
  <script type="text/javascript">  
     $(document).ready( function(){            
      $("#attach").after("<input id='fakeAttach' type='button' value='attach a file' />");      
      $("#fakeAttach").click(function() {            
        $("#attach").click();        
        $("#maxSize").after("<div id='temporary'><span id='attachedFile'></span><input id='remove' type='button' value='remove' /></div>");        
        $('#attach').change(function(){
          $("#fakeAttach").attr("disabled","disabled");          
          $("#attachedFile").html($(this).val());
        });        
        $("#remove").click(function(e){
          e.preventDefault();
          $("#attach").replaceWith($("#attach").clone());
          $("#fakeAttach").attr("disabled","");
          $("#temporary").remove();
        });
      })
    }); 
  </script> 
</head>
<body>
  <input id="attach" type="file" /><span id="maxSize">(less than 1MB)</span>    
</body>
</html>
+4  A: 

You can't access input=file elements from JavaScript for Security reasons. JavaScript is not allowed to read the files on users drive and type=file inputs allow you to do this.

if you allowed people access to file inputs with javascript there is nothing to stop them from copying your password file up and hacking it.

I found this article that describes JavaScript security

AutomatedTester
I know, I know, but I'm speaking about IE. Try with something simpler and see how it can access input=file indeed. For example:<input type="file" /><span>Here goes the name of the file</span>//get file inputvar $el = $('input[type=file');//set the next siblings (the span) text to the input value $el.next().text( $el.val() );
My answer is for all browsers. The only way you **may** get around it is if you run it in a IEHTA or Firefox Chrome window but I think you will still get stopped by the Browser/JavaScript Security
AutomatedTester
Please copy and paste the code above and run it in IE6 or IE7. As you can see, IE lets me trigger the input="file". However, I can't get to update the <span> so it shows the file browsed.
its not firing because of JavaScript security
AutomatedTester
You didn't copy and paste, did you? =) Be sure of testing in IE and update the jquery.js path. I just test it again and it triggered the browse dialog as I told you. Still stuck on the <span> issue.
A: 

You can't invoke the file upload via javascript. I know, you should be able to, oh well. There is a solution - using opacity:0 to 'hide' the file input, put something under it, so it looks like you are clicking on something else.

PPK shows you how do to it and goes through all the ins and outs on quicks blog

Steve Brewer
Oh, you can in IE. As simple as doing a copy-paste to see it (just in IE). This approach is for IE only (no for Firefox).