检查fileupload的大小(验证) [英] checking the size of fileupload (validation)

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

问题描述

heyy ..



我使用文件上传控件,在这里我要检查,上传的文件大小不应超过4MB ...

是否有任何JavaScript可以处理这个....上传时间或选择文件时..

heyy..

Iam using a file-upload control ,here I want to check , uploaded file size should not exceed than 4MB...
Is there is any JavaScript to handle this ....while upload time or while select the file..

推荐答案

在cs文件中尝试此操作

try this in cs file
 if(  FileUpload.PostedFile.ContentLength >5000)
{
return  "File exceed";
}


- MAX_FILE_SIZE字段: http ://www.tizag.com/htmlT/htmlupload.php [ ^ ]。但并非所有浏览器都支持它。

- File Api [ ^ ]。仅适用于某些现代浏览器,请参见此处的示例: http://stackoverflow.com/questions/3717793 / javascript-file-upload-size-validation [ ^ ]

- 使用插件,如下所示: swfupload [ ^ ]



所有这些都可以在客户端禁用或解决。因此,您应始终在服务器端处理文件大小检查。这在asp.net和iis中并不像在apache上的php那样容易。

首先,您可以在iis中设置文件上传限制: http://www.webtrenches.com/post.cfm/iis7-file-upload-size-limits [ ^ ]。

但这不是全部。



如果限制较小,你可以简单地将iis限制设置为略高于4MiB(限制本身不是附件大小,它是对于整个请求),并从代码检查大小 - 如有必要,重定向到错误页面。



如果文件非常大或连接速度慢,你必须还要考虑请求处理超时,你需要一个http处理程序模块来完成工作,如下所述:http://www.ideosity.com/ourblog/post/ideosphere-blog/2009/11/17/handling- (和压缩)-large-file-uploads-in-asp.net [ ^ ](这是vb.net,但并不难录制)。你也可以使用这种方法。
- MAX_FILE_SIZE field: http://www.tizag.com/htmlT/htmlupload.php[^]. But it is not supported by all browsers.
- File Api[^]. Only working in some modern browsers, see sample here: http://stackoverflow.com/questions/3717793/javascript-file-upload-size-validation[^]
- Using a plugin, like this one: swfupload[^]

All these can be disabled or worked around on client side. Thus you should always handle file size checking also on server side. This is not as easy in asp.net and iis as it is in php on apache.
First of all, you can set file upload limit in iis: http://www.webtrenches.com/post.cfm/iis7-file-upload-size-limits[^].
But that''s not all.

In case of smaller limits, you can simply set up the iis limit to a little bit higher than 4MiB (the limit itself is not for the attachment size, it is for the whole request), and check size from code - redirect to an error page if necessary.

In case of really large files, or slow connection, you have to consider also the request processing timeout, and you will need a http handler module to do the work, like described here: http://www.ideosity.com/ourblog/post/ideosphere-blog/2009/11/17/handling-(and-squashing)-large-file-uploads-in-asp.net[^] (this is vb.net, but not hard to transcribe). You can use this approach in your case too.


http:/ /msdn.microsoft.com/en-us/library/aa479405.aspx [ ^ ]


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

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