> 文档中心 > 解决IIS EXPRESS文件上传限制的问题

解决IIS EXPRESS文件上传限制的问题

文章目录

  • 前言
    • ASP.NET开发中使用到FileUpload控件来进行上传文件时,上传文件大小受到来自三个方面控制。①页面自定义程序逻辑②应用程序Web.config③IIS服务器配置文件applicationhost.config。
  • 一、页面调试的出错信息
  • 二、网上资料查阅
    • 1.关键词选择
    • 2.根据文章描述应该是服务器IIS7对上传有requestLimits 项
  • 三、案例实践
    • 1.上传页面设计界面
    • 2.上传页面:源
    • 3.后台代码
    • 4.web.config文件配置
    • 5.小技巧:生成固定大小测试文件
  • 总结

前言

ASP.NET开发中使用到FileUpload控件来进行上传文件时,上传文件大小受到来自三个方面控制。①页面自定义程序逻辑②应用程序Web.config③IIS服务器配置文件applicationhost.config。

提示:以下是本篇文章正文内容,下面案例可供参考

一、页面调试的出错信息

请求筛选模块被配置为拒绝超过请求内容长度的请求。
可尝试的操作:

  • 确认 applicationhost.config 或 web.config 文件中的 configuration/system.webServer/security/requestFiltering/requestLimits@maxAllowedContentLength 设置。

在这里插入图片描述

二、网上资料查阅

1.关键词选择

requestLimits  maxAllowedContentLength

查阅到的比较贴近的例子链接
https://blog.csdn.net/yw1688/article/details/49070633
在这里插入图片描述

2.根据文章描述应该是服务器IIS7对上传有requestLimits 项

文件位置:C:\Windows\System32\inetsrv\config\applicationhost.config
配置节内容:(注意单位是:字节)

<system.webServer>   <security><requestFiltering><requestLimits maxAllowedContentLength="40000000" /></requestFiltering>   </security><system.webServer>

本实例在VS2015环境里测试,使用的是IIS EXPRESS,与IIS7有区别。
IIS EXPRESS配置文件位置:

C:\Program Files (x86)\IIS Express\AppServer\applicationhost.config

三、案例实践

根据文章内容进行反复实践,发现因为IIS和IIS EXPRESS的不同,并不能得到正确的结果,需要进行调整才能最终完成,后面附能正常处理完整案例代码。

1.上传页面设计界面

解决IIS EXPRESS文件上传限制的问题

2.上传页面:源

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo09_UpLoad.aspx.cs" Inherits="WebApp.Demo09_UpLoad" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title></head><body>    <form id="form1" runat="server">    <div>    请选择要上传的文件:<asp:FileUpload ID="ful" runat="server" /> &nbsp;&nbsp; <asp:Button ID="btnUpload" runat="server" Text="开始上传" OnClick="btnUpload_Click" /> <br /> <br /> <asp:Literal ID="ltaMsg" runat="server"></asp:Literal>    </div>    </form></body></html>

3.后台代码

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebApp{    public partial class Demo09_UpLoad : System.Web.UI.Page    { protected void Page_Load(object sender, EventArgs e) { } protected void btnUpload_Click(object sender, EventArgs e) {     //【1】判断文件是否存在     if (!this.ful.HasFile) return;     //【2】获取文件大小,判断是否符合设置要求(变成MB)     double fileLength = this.ful.FileContent.Length / (1024.0 * 1024.0);     //获取配置文件中上传文件大小的限制     double limitedLength = Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["PhysicsObjectLength"]);     limitedLength = limitedLength / 1024.0;//转换成MB单位     //判断实际文件大小是否符合要求     if (fileLength > limitedLength)     {  //  this.ltaMsg.Text = "上传文件大小不能超过" + limitedLength + "MB";  this.ltaMsg.Text = "alert('上传文件最大不能超过" + limitedLength + "M')";  return;     }     //【3】获取文件名,判断文件扩展是否符合要求     string fileName = this.ful.FileName;     //判断文件名是否是exe文件     if (fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower() == "exe")     {  this.ltaMsg.Text = "alert('上传文件不能是exe文件')";  return;     }     //修改文件名       fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + fileName;      //fileName = DateTime.Now.ToString("yyyyMMddhhssms") + "_" + fileName;     // (yyyyMMddHHmmssfff     //【4】获取服务器文件夹路径      string path = Server.MapPath("~/UploadFiles"); //【5】上传文件     try     {  this.ful.SaveAs(path + "/" + fileName);  this.ltaMsg.Text = "alert('文件上传成功!')";     }     catch (Exception ex)     {  this.ltaMsg.Text = "alert('文件上传失败!" + ex.Message + "')";     } }    }}

4.web.config文件配置

<?xml version="1.0" encoding="utf-8"?><!--  有关如何配置 ASP.NET 应用程序的详细信息,请访问  http://go.microsoft.com/fwlink/?LinkId=169433  --><configuration>  <appSettings>    <!--配置上传文件最大字节数:单位KB-->    <add key="PhysicsObjectLength" value="30720"/>  </appSettings>    <system.web>      <!--设置请求的最大字节数(默认是4096,单位:KB)-->      <httpRuntime maxRequestLength="40960"></httpRuntime>      <compilation debug="true" targetFramework="4.0" />    </system.web>  <system.webServer>    <security>      <requestFiltering> <requestLimits maxAllowedContentLength="60000000" />      </requestFiltering>    </security>  </system.webServer>  

5.小技巧:生成固定大小测试文件

使用WinRAR可以轻松得到固定大小的压缩包文件,注意没必要等到全部生成就可以取消掉这个操作。因为这个操作将生成一系列固定大小的文件。而用于测试,1个文件就够了。
在这里插入图片描述
在这里插入图片描述

总结

读者应该能够发现笔者的配置configuration/system.webServer/security/requestFiltering/requestLimits由applicationhost.config转放到了web.config文件。这需要applicationhost.config文件的配置节作相应的设置configuration/configSections/sectionGroup/section

<section name="requestFiltering" overrideModeDefault="Allow" />

这项设置在IIS7中是Deny,但在IIS EXPRESS中默认为Allow,其实应该能想明白,为了减少调试的不确定性,VS2015用这项设置,把文件上传限制的权限放到了web.config文件里。
另外,在作配置时应该有一个梯度:

①页面自定义文件限制≤②应用程序文件限制≤③IIS服务器文件限制

服务器123