0

I have a simple function with I can upload a file via a webpage. When I upload file with size 16 MB, it works, but if I try with a bigger file eg. 57 MB, the program throws
Stream was too long exception.

Stream memStream = new MemoryStream();            

for (int i = 0; i < files.Length; i++)
            {
                memStream.Write(boundarybytes, 0, boundarybytes.Length);
                var header = string.Format(headerTemplate, "uplTheFile", files[i]);
                var headerbytes = Encoding.UTF8.GetBytes(header);

                memStream.Write(headerbytes, 0, headerbytes.Length);

                using (var fileStream = new FileStream(files, FileMode.Open, FileAccess.Read))
                {
                    var buffer = new byte[fileStream.Length];
                    var bytesRead = 0;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        memStream.Write(buffer, 0, bytesRead);
                    }

                }
            }

            memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            request.ContentLength = memStream.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                memStream.Position = 0;
                byte[] tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();
                requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            }

            using (var response = request.GetResponse())
            {
                Stream stream2 = response.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
                return reader2.ReadToEnd();
            }

I initialize the request with that way.

    HttpWebRequest request = HttpWebRequest.Create(url);
    request.Timeout = 3600000;
    request.Method = "POST";
    request.KeepAlive = true;
roll
  • 117
  • 7
  • Does the below link help ? https://stackoverflow.com/questions/3853767/maximum-request-length-exceeded – Vamsi Jul 17 '18 at 09:32
  • Hi, The server is not on my computer. – roll Jul 17 '18 at 10:34
  • IIS by default has a kinda low size limit (20 or 30 MB if I recall correctly). You should notice that it is a server setting, and cannot be bypassed at client side. Yous should consider asking the server admin to parametrize a higher limit – Cleptus Jul 17 '18 at 11:47

2 Answers2

0

So the server you are connecting to has the limits then ? In which case you can't upload it anyways right ?

Vamsi
  • 11
  • 2
0

for (int i = 0; i < files.Length; i++) was the problem.

roll
  • 117
  • 7