【问题标题】:FileStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException' in C# MVCFileStream.ReadTimeout' 在 C# MVC 中引发了“System.InvalidOperationException”类型的异常
【发布时间】:2021-10-20 08:21:39
【问题描述】:

我正在尝试将文件发送到公共 IP。它在文件大小较小但我想发送较大的 .BAK 文件时工作。虽然我正在尝试发送少于 20-30 MB 的 .BAK 文件,但它的作品但不超过 30MB。
我尝试过:- 在 Web.config 中

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="2147483648" />
    </requestFiltering>
  </security>
</system.webServer>
 //this also tried
<system.web>
<httpRuntime maxRequestLength="2097152" />
<compilation>
  <assemblies>
    <add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD324E38" />
  </assemblies>
</compilation>

- 我尝试增加 IIS 中的最大上传文件大小

           string publicAPI = "https://localhost:44350/Upload/";
            var directory = new DirectoryInfo("D:\\Database");
            var fileName = (from f in directory.GetFiles()
                          orderby f.LastWriteTime descending
                          select f).First();

            Stream fileStream = System.IO.File.OpenRead(fileName.FullName); // here is error:ReadTimeout = 'fileStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
            try
            {
                using (var client = new HttpClient())
                {
                    using (var formData = new MultipartFormDataContent())
                    {
                        formData.Add(new StreamContent(fileStream), "Mytic", fileName.Name);
                        var response = client.PostAsync(publicAPI, formData).Result;
                        if (response.IsSuccessStatusCode )
                        {}
                        else
                        {}
                    }
                }
            }

我在 fileStream 中遇到错误(error: ReadTimeout = 'fileStream.ReadTimeout' 引发了类型为 'System.InvalidOperationException' 的异常) .如何读取大文件(.BAK)?

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-core asp.net-web-api


    【解决方案1】:

    对您的代码进行了一些更改。让我知道这是否有效。

      string publicAPI = "https://localhost:44350/Upload/";
            var directory = new DirectoryInfo("D:\\Database");
            var fileName = (from f in directory.GetFiles()
                            orderby f.LastWriteTime descending
                            select f).First();
    
            Stream fileStream = System.IO.File.OpenRead(fileName.FullName); // here is error:ReadTimeout = 'fileStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
            try
            {
                HttpClientHandler handler = new HttpClientHandler();
                handler.MaxRequestContentBufferSize = 50000*1024;
    
                using (var client = new HttpClient(handler))
                {
    
                    using (var formData = new MultipartFormDataContent())
                    {
                        formData.Add(new StreamContent(fileStream), "Mytic", fileName.Name);
                        var response = client.PostAsync(publicAPI, formData).Result;
                        if (response.IsSuccessStatusCode)
                        { }
                        else
                        { }
                    }
                }
            }
            catch (Exception ex)
            { 
            }
    

    在接收端,我将请求长度增加到 700 mb。我测试我可以上传大文件。下面是我的网络配置。尝试增加请求长度,如下所示。

    【讨论】:

    • 不工作@saifiqbal。 Stream fileStream = System.IO.File.OpenRead(fileName.FullName) ,我在这里遇到错误。
    • 您必须发布完整的错误,以便每个人都能理解您的问题。
    • 这只是错误:ReadTimeout = 'fileStream.ReadTimeout' 引发了“System.InvalidOperationException”类型的异常
    猜你喜欢
    • 1970-01-01
    • 2020-04-04
    • 2018-12-03
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多