Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    that.type.match(/text.*/) fails

    In the following script, the line
    that.type.match(/text.*/)
    is failing, or at least not returning the result I would expect.

    Code:
    if(that.files && that.files[0]){
    	var reader = new FileReader();
    	var output;
    	var textType = /text.*/;
    
    	var f = that.files[0];
    	
    // I've tested with .txt and .jpg files.
    // in both cases "!that.type.match(/text.*/)" returns true.
    
    // What am I doing wrong? Or don't I understand what I'm 
    // doing?
    	
    	if(!that.type.match(/text.*/)) {
    		var name = f.name;
    		var msg = "Obituary file must be a text file.<br />";
    		msg += "File " + name + " is not a text file.";
    			
    		document.getElementById('errdisplay').innerHTML = msg;
    	}
    }

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    <input type=file>
    Has many restrictions
    due to browser security,
    this may be your problem ?

    Or more likely I have
    miss read you question,
    sorry.
    Last edited by DaveyErwin; 12-24-2013 at 03:57 PM.

  • #3
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am trying to determine if the file that is being uploaded is a text file. Right now I am testing under "localhost." If I delete this "match()", I have no problem uploading the file. It is the "match()" that is always returning false whether the file is a text file or an image file.

    Hope this explains better.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,769
    Thanks
    55
    Thanked 518 Times in 515 Posts
    can I ask what is "that" in your code?

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Okay, after actually reading
    your post, your code is checking
    if the file name is text without
    regard to extention, it is not
    checking the "type".

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,769
    Thanks
    55
    Thanked 518 Times in 515 Posts
    I think it is checking the type OK, but it seems to work better (but not perfectly) like this...

    Code:
     <body>
     <input type="file" onchange="pickIt(this.files)"/> 
     <div id="errdisplay"></div>    
        
     <script>
     function pickIt(that){
    if(that && that[0]){
    	var reader = new FileReader();
    	var output;
    	var textType = /text.*/;
    
    	var f = that[0];
    	
    	if(!f.type.match(/text.*/)) {
    		var name = f.name;
    		var msg = "Obituary file must be a text file.<br />";
    		msg += "File " + name + " is not a text file.";
    			
    		document.getElementById('errdisplay').innerHTML = msg;
    			}
    		}
    	}
        
      </script>     
      
    </body>

  • Users who have thanked xelawho for this post:

    DaveyErwin (12-24-2013)

  • #7
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. This change fixed the problem.

    var f = that[0];

    if(!f.type.match(/text.*/)) {


    Thanks again.

    Is there a way to close this thread as "solved?"

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Hello Alex

    Again, I have miss read.
    Still in does not check the actual type.
    A binary file with the extension .txt
    will pass the test while a text file
    with the extension .js will fail.
    Last edited by DaveyErwin; 12-24-2013 at 05:36 PM.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    you can also use the accept attribute to help cut down on the need for onchange validation while thinning the filelist for the user, make the app quicker and easier to use.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •