Jump to content

Testing File Types -- file.type.match(patt)


davej

Recommended Posts

I don't see it described here... so maybe it is HTML5 specific... http://www.w3schools..._obj_regexp.asp ...but I am trying to use file.type.match(regex_pattern) with a complete lack of understanding.What does this test tell you? I was just trying to read in a .csv text file. My example code shows...

function handleFile(inputFile) {var csvMimeType = /text\/csv/;var file = inputFile.files[0];//verify the file is a csv fileif ( file.type.match(csvMimeType) ) {alert('accept');}else{alert('reject');}...}

After struggling with various other regex values from other examples I finally bypassed the test and got a result that says: File type: application/vnd.ms-excel So is this baloney test feature just testing the file association for the .csv file extension? If so that is just worthless garbage. Perhaps .csv files are associated with Excel on this machine, but who cares? I see the match feature is used in the following examples... http://msdn.microsof...7(v=vs.85).aspx and http://www.htmlgoodi...filereader.html What good is identifying the associated application inside Javascript? In my situation I just want to make sure the file extension is .csv and that the contents of the file do not contain non-text characters. Shouldn't that be the goal?

Edited by davej
Link to comment
Share on other sites

Yes, the mime type is basically browser-specific. A browser can send any value it wants for a mime type based on the settings on the computer. You'll need to test the extension if you want to do that, I prefer to do all file tests on the server instead of through Javascript.

Link to comment
Share on other sites

Yes, the mime type is basically browser-specific. A browser can send any value it wants for a mime type based on the settings on the computer. You'll need to test the extension if you want to do that, I prefer to do all file tests on the server instead of through Javascript.
Well, I can't even use this to filter the choices that show up in the popup file selector. I am looking here: http://www.w3schools...nput_accept.aspandhttp://www.iana.org/...text/index.html But I can't get *.csv files to show up in Chrome unless everything else does. input accept="text/csv" shows all files and "text/*" does not show *.csv files. Edited by davej
Link to comment
Share on other sites

Again, since mime types are system-dependent, you'll need to search for multiple possibilities to catch what the browser may consider a CSV file, e.g.: text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel Those aren't going to be limited to CSV files, that would include any Excel file for example, but since Excel registers itself as a handler for CSV files then it sets that mime type if you have it installed. Because of varying mime types, the accept attribute would be much more useful if you could also specify valid extensions.

Link to comment
Share on other sites

I did finally get it to work. I found this article... http://stackoverflow...te-is-it-useful

<input type="file" id="infile" onchange="handleFile(this);" accept="text/*,.csv"></input>

Now why does a comma work when I think you are supposed to use a vertical bar?

Edited by davej
Link to comment
Share on other sites

Are you asking why a comma works when that is the designation of the format? Comma Separated Values? Also, the vertical bar is referred to as a pipe.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...