Search code examples
c#.netwinformsupload

.NET Winforms c# Upload large files


I get this error when file length is higher than 18 mb:

stream does not support concurrent IO read or write operations

The error is localized in this section:

requestStream.Write(header, 0, header.Length);
if (fileData != null)
{
     // write the file data, if any
     byte[] buffer = new Byte[checked((uint)Math.Min(8192, (int)fileData.Length))];
     int bytesRead;

     try
     {
          while ((bytesRead = fileData.Read(buffer, 0, buffer.Length)) != 0)
          {
               //throws an exception after 3000 loops aprox
               requestStream.Write(buffer, 0, bytesRead);
          }
     }
     catch (Exception ex)
     {
          string aux = ex.Message;
     }
}

// write footer
requestStream.Write(footer, 0, footer.Length);
return webrequest.GetResponse();

I tried with lock sentence, sendChunked = true and AllowWriteStreamBuffering = false But I can't upload files correctly.

Thanks in advance


Solution

  • Maybe the web server sends an error message (read) that the size limit has exceeded while the loop tries to upload (write) data to the stream. You can try to check what is sent between the server and the program with Wireshark or some other network sniffer.