asp.net 限制上传文件的大小与时间

在web.Config文件中配置限制上传文件大小与时间的字符串是在<httpRuntime><httpRuntime/>节中完成。

maxRequsetLength 属性:用于防止服务攻击,例如,因用户向服务器发送大型文件而导致的拒接访问。默认值为4096kb(4M)

ExecutionTimeout 属性:指定在Asp.Net 应用程序自动关闭前,允许执行请求的最大秒数。只有当compilation 元素中的调试属性为False,此超时属性才适用。默认值为110s

在Web.Config文件中的<system.web></system.web>节中添加如果下代码以限制上传文件最大为4Mb,网页超时时间为100s

<httpRuntime maxRequestLength="4096" executionTimeout="100"/>

接着在上传方法中写如下代码:

protected Void ImgBtnSeng()

{

  string filepath=FileUp.PostedFile.FileName;

  string fileName=fiepath.Substring(filepath.LastIndexOf("\\")+1);

  string serverpath=Sever.MapPath("AttachFiles/")+filename;

  FileUP.PostedFile.SaveAs(serverpath);

}