FileStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException' in C# MVC

Viewed 1319

I am trying to send file to public IP. It works when file size is low but i want to send .BAK file which is large in size. While i am trying to send less than 20-30 MB of .BAK file its works but not more than 30MB.
I tried:- in 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>

</system.web>

- I tried increased in the maximum upload file size in 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
                        {}
                    }
                }
            }

I am getting error in fileStream(error: ReadTimeout = 'fileStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException') . How can i read Large file(.BAK)?

1 Answers

Done some changes in your code. Let me know if this works.

  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)
        { 
        }

on the receiving end i have increase the request length equal to 700 mb. I tested i can upload large file. Below is my web config. Try increasing the request length as shown below.

enter image description here

Related