验证用JavaScript / jQuery的文件上传 [英] Validate file upload with javascript/jquery

查看:98
本文介绍了验证用JavaScript / jQuery的文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是令人沮丧的。我已经搜查如何验证选定的文件对谷歌有很多(上传)扩展和大小,但文件大小问题上没有取得成功。我在asp.net图片上传控件(指上传图片),但是扩展和文件大小在服务器上进行验证。这就是它是如何工作的。如果用户提交的是电影更名为.JPG 1GB的文件?

Ok, this is frustrating. I have searched a lot on google on how to validate selected file (to upload) extension and size but no success on the file size matter. I have an image upload control in asp.net (meant to upload images) but the extension and file size is validated on the server. That's how it works. What if the user submits a 1GB file that is a movie renamed to .jpg?

不知怎的,微博,例如管理,以验证这两个扩展客户端上的输入文件和文件大小。任何人有任何想法如何做到这一点提交给服务器之前?

Somehow Twitter for example managed to validate the input file on client side, both extensions and file size. Anyone got any ideas how to do that before submitting to the server?

谢谢!

LE:不,不,HTML5。这不是在所有的浏览器都支持。我需要一个解决方案在IE 7和8也工作了!

LE: No, no HTML5. It's not supported in all browsers. I need a solution to work in IE 7 and 8 also!

推荐答案

如果用户浏览器支持HTML5的 fileapi ,则可以从文件中读取输入文件大小。

If the user browser supports the html5 fileapi, you can read a file size from the file input.

document.getElementById('file').files[0].size  //size of file in bytes
document.getElementById('file').files[0].name  //name of file
document.getElementById('file').files[0].type  //mime type

这篇关于验证用JavaScript / jQuery的文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆