Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何读取maxAllowedContentLength_Asp.net_Iis 7_File Upload_Uploadify - Fatal编程技术网

Asp.net 如何读取maxAllowedContentLength

Asp.net 如何读取maxAllowedContentLength,asp.net,iis-7,file-upload,uploadify,Asp.net,Iis 7,File Upload,Uploadify,对于我的web应用程序,我有用于上传文件的flash组件。我想在客户端处理最大文件大小限制,而不实际将该文件发送到服务器。所以我需要以某种方式从配置文件中读取该值,然后将其发送到客户端。我发现的一些文章说,直接读取配置文件不是解决方案,因为它可以在很多地方更改。所以可能会有一些API调用,但我找不到任何 <system.webServer> <security> <requestFiltering> <re

对于我的web应用程序,我有用于上传文件的flash组件。我想在客户端处理最大文件大小限制,而不实际将该文件发送到服务器。所以我需要以某种方式从配置文件中读取该值,然后将其发送到客户端。我发现的一些文章说,直接读取配置文件不是解决方案,因为它可以在很多地方更改。所以可能会有一些API调用,但我找不到任何

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

试试这种方法

您可以根据配置文件中的web配置更改下面的代码段

我的web.config有些看起来像

<system.web>
  <httpRuntime executionTimeout="30"  maxRequestLength="100"/>

您可以使用HttpRunnesce实例设置值。

我知道这是一个老问题,但它花费了我太多的时间(浪费了),我想为可能遇到我的人发布一个可行的解决方案:

Using Microsoft.Web.Administration;

uint uiMaxAllowedContentLength = 0;
using (ServerManager serverManager = new ServerManager())
{
    Configuration config = serverManager.GetWebConfiguration("Default Web Site/{{your special site}}");
    ConfigurationSection requestFilteringSection = config.GetSection("system.webServer/security/requestFiltering");
    ConfigurationElement requestLimitsElement = requestFilteringSection.GetChildElement("requestLimits");
    object maxAllowedContentLength = requestLimitsElement.GetAttributeValue("maxAllowedContentLength");
    if (null != maxAllowedContentLength)
    {
        uint.TryParse(maxAllowedContentLength.ToString(), out uiMaxAllowedContentLength);
    }

}
请确保首先下载并安装Microsoft Web管理软件包 (

PM>安装软件包Microsoft.Web.Administration

)

此外,您可能需要调整web.config文件的权限。至少授予IUSR和IIS_IUSR“读取”权限

这段代码实际上来自微软的网站,但要找到它却花了很长时间!希望我能帮你节省几个小时

干杯


Roman不带Microsoft Web管理软件包:

using System.Web.Configuration;
using System.Configuration;
using System.Xml;
using System.IO;

Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
IgnoreSection ignoreSection = configuration.GetSection("system.webServer") as IgnoreSection;
string sectionXml = ignoreSection.SectionInformation.GetRawXml();
StringReader stringReader = new StringReader(sectionXml);
XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
UInt32 maxAllowedContentLength = 0;
if(xmlTextReader.ReadToDescendant("requestLimits"))
    UInt32.TryParse(xmlTextReader.GetAttribute("maxAllowedContentLength"), out maxAllowedContentLength);

我不确定是否应该否决这个答案,因为这是为了阅读“Max ReavestStudio”(从<代码>系统.Web/HutpunTime<代码>)而不是“Max LoopEdCeTeNeLead”(从<代码>系统.WebServer / Survivs/ReestSimult/RealStestBuffs>代码>)我认为它可以,因为可以设置两个设置(Max ReavestLime&Max PosivCordTeNoST)。设置为相应的(相同)值,并仅使用此代码跳过安装“Microsoft.Web.Administration”包。
using System.Web.Configuration;
using System.Configuration;
using System.Xml;
using System.IO;

Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
IgnoreSection ignoreSection = configuration.GetSection("system.webServer") as IgnoreSection;
string sectionXml = ignoreSection.SectionInformation.GetRawXml();
StringReader stringReader = new StringReader(sectionXml);
XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
UInt32 maxAllowedContentLength = 0;
if(xmlTextReader.ReadToDescendant("requestLimits"))
    UInt32.TryParse(xmlTextReader.GetAttribute("maxAllowedContentLength"), out maxAllowedContentLength);