在输入文件中获取zip文件的文件类型(Getting the file type of a zip file in input file)

编程入门 行业动态 更新时间:2024-10-05 23:28:24
在输入文件中获取zip文件的文件类型(Getting the file type of a zip file in input file)

我试图在input[type=file]更改事件上捕获一个文件:

这是我的HTML:

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

这是我的JavaScript(使用jQuery):

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

它与图像,txt,doc,docx,xl​​sx等等完全正常,但不与zip文件。 当我尝试使用zip文件时,var文件包含一个类型属性为空。

你知道我为什么以及如何得到它?

编辑1我在Windows 7中使用Chrome浏览器。我的文件用7zip压缩。

编辑2这里是我得到的:

I am trying to catch a file on a input[type=file] 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 || e.target).files[0]; console.log(e); }

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?

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

EDIT 2 here is what I get :

最满意答案

你的代码有效。

我尝试了enapupe的小提琴,它起作用http://jsfiddle.net/rvd6T/

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

我的控制台日志 在此处输入图像描述


您应该知道类型会有所不同。

有时它会是类型:“application / x-zip-compressed”“application / zip”

你最有可能只是以文件名为基础,* .zip(以zip和case不敏感结尾)

但是如果它实际上是一个ZIP文件且无法解压,仍然必须在服务器端检查它

Your code works.

I tried enapupe's fiddle and it works http://jsfiddle.net/rvd6T/

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

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

更多推荐

本文发布于:2023-07-19 15:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   文件类型   zip   input   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!