BastienSander BastienSander - 1 year ago 56
jQuery Question

Getting the file type of a zip file in input file

I am trying to catch a file on a

change event :

Here is my HTML :

<input type="file" id="file-upload" name="file-upload">

Here is my JavaScript (using jQuery) :

$('#file-upload').bind("change", function(e){
var file = (e.srcElement ||[0];

It works fully fine with images, txt, doc, docx, xlsx etc etc etc BUT NOT with zip files.
When I try it with a zip file, the var file contains a type attributes empty.

Do you have an idea why and how i could get it?

I use Chrome Browser, in Windows 7. My files are zipped with 7zip.

here is what I get :

Console log

Answer Source

Your code works.

I tried enapupe's fiddle and it works

$('#file-upload').bind("change", function(e){
var file = (e.srcElement ||[0];

My console log enter image description here

You should be be aware that the types will vary.

sometimes it would be type: "application/x-zip-compressed" or "application/zip"

You most likely should just base it on the filename instead, *.zip (ends with zip & case insensitive)

but still must check it on server side if its actually a ZIP file and unzipable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download