检查上传的文件大小。 [英] Check uplaoded file size.

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

问题描述





我写了一个上传文件的应用程序,该文件用于保存在服务器上。

使用MVC3和简单的HTML输入文件控件来做这件事。



问题:当我上传大文件(超过5 MB) )浏览器显示此页面无法显示。和应用程序崩溃。



原因:这是因为IIS拒绝了请求。



我尝试了什么:我有在Controller中编写代码来处理这个条件,但它没有调用,因为IIS没有将请求传递给服务器。



思考:我们可以有机制检查

1.检查客户端大小的文件大小(通过jQuery,JS等)?

2.编写可以在IIS拒绝之前执行的代码。

3. HTTP处理程序。



我只是想查看上传的文件大小,应该提示用户无法上传超过1 MB大小的文件。



请提供建议,我该如何实现。



谢谢。

解决方案

不,jquery不是魔术,它无法访问您的文件系统。您可以设置Web配置以设置最大文件大小,但如果文件太大,则无法更改将超时。您最好的选择可能是使用某种第三方文件上传控件,Flash可以检查客户端上的文件大小。


Refer- 使用jQuery检查文件输入大小 [ ^ ]。

引用:

您实际上无法访问文件系统(例如,读取和写入本地文件),但是,由于HTML5文件Api规范,您可以访问一些文件属性,文件大小就是其中之一。



对于下面的HTML

 <  输入   类型  =  file    id   =  myFile    /  >  



尝试以下方法:

  //  绑定到输入字段的onchange事件 


' #myFile')。bind(' change'功能(){

// this.files [0 ] .size获取文件的大小。
alert( this .files [ 0 ].size);
});



因为它是HTML5规范的一部分,它只适用于现代浏览器(IE需要v10),我添加了这里有关您应该了解的其他文件信息的更多详细信息和链接: http://i.ndigo.com.br/2012/01/javascipt-checking -the-文件大小/


Hi,

I have written an application which upload a file and that file is use to save on server.
Have used MVC3 with simple HTML input file control to do this.

Problem: When I upload the large file(more than 5 MB) the browser shows this page cant display. and application get crashed.

Reason : This is because the IIS is decline the request.

What I tried : I have written code in Controller to handle this condition but its not calling since IIS is not passing request to server.

Thinking : Can we have mechanism to check
1. Check the file size at client size(through jQuery, JS etc)?
2. Write code which can execute before IIS decline.
3. HTTP handler.

I just want to check the uploaded file size and there should be a prompt that user cannot upload file more than 1 MB size.

Please provide suggestions, how can I achieve this.

Thanks.

解决方案

No, jquery is not magic, and it cannot access your file system. You can set up your web config to set a max file size, but you can't change that there will be a timeout if the file is too big. Your best bet is probably to use some sort of third party file upload control, a Flash one can check the file size on the client.


Refer- Check file input size with jQuery[^].

Quote:

You actually don't have access to filesystem (for example reading and writing local files), however, due to HTML5 File Api specification, there are some file properties that you do have access to, and the file size is one of them.

For the HTML bellow

<input type="file" id="myFile" />


try the following:

//binds to onchange event of your input field


('#myFile').bind('change', function() { //this.files[0].size gets the size of your file. alert(this.files[0].size); });


As it is a part of the HTML5 specification, it will only work for modern browsers (v10 required for IE) and I added here more details and links about other file information you should know: http://i.ndigo.com.br/2012/01/javascipt-checking-the-file-size/


这篇关于检查上传的文件大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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