0

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?

Thanks!

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

4

2 回答 2

3

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

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
于 2013-03-15T18:41:57.227 回答
0

正如 Musa 所指出的,HTML5 api 可能会过滤掉大多数情况。

也有专有的 ActiveX 方法可以做到这一点,但支持有点未知。

如果您坚持不使用 html5,Flash 可以为您检查,您可以查看类似 swfupload https://code.google.com/p/swfupload/

于 2013-03-15T18:57:16.990 回答