huoxuanya

   限制上传文件的大小我们可以再服务端使用代码实现如下:

     if (FileUpload1.PostedFile.ContentLength > 4096)
            {
                LblMessage.Text = "文件大小不能超过网站配置文件中默认的4MB";
            }  但是这需要再每个需要上传的页面添加这个代码,今天发现有一个简单的方法来实现限制文件上传的大小。

 再web.config的<system.web>节点中配置<httpRuntime>节点的maxRuquestLength属性,默认是4096KB;

ExecutionTimeout属性,允许执行请求的最大秒数,只有当compilation元素中的调试属性为False时,此超时属性才适用,默认为100s.

如下:<system.web>
          <compilation debug="true" targetFramework="4.0" />
          <httpRuntime maxRequestLength="4096" executionTimeout="100"/>
        </system.web>

为页面上的“发送”按钮添加点击事件,在事件中添加如下代码:

                string filepath = FileUpload1.PostedFile.FileName;//得到文件路径名
                string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                string loadpath = Server.MapPath("UpLoad/") + filename;
                FileUpload1.PostedFile.SaveAs(loadpath);
                LblMessage.Text = "文件上传成功";

分类:

技术点:

相关文章:

  • 2021-09-10
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-11
  • 2021-10-05
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
相关资源
相似解决方案