FileUploaded - InputStream issue

2 posts, 0 answers
  1. Kevin J
    Kevin J avatar
    18 posts
    Member since:
    Jun 2008

    Posted 21 Oct 2016 Link to this post

    Basically, I'm trying to split uploaded files into 4meg chunks and write them out.  However, I'm getting an error and can't figure out why.

    The file being uploaded is 65meg.

    Below is my source code.  It creates the first Chunk fine.  It then loops around, and when it starts the second Chunk, it blows up with this error message:
    "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."

    The line of code that is the offender is inside the Else statement and is:

                            streamFile.Read(chunk, index, intChunkLength);

    index is 4194304 bytes (4,194,304)
    intChunkLength is also 4194304 bytes (4,194,304)
    streamFile.ContentLength is 95724958 (95,724,958)


    Now, the way I understand it (Which could be wrong) is that streamFile should be reading the uploaded file starting at the 4194304 byte, and reading in an additional 4194304 bytes.  That would be 8,388,608 bytes in.   That is well within the 95,724,958 bytes that was uploaded.   So why am I getting this error?  Maybe I don't understand the way InputStream is supposed to work. 

            protected void RadAsyncUpload1_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs args)
                // This is an experiment to break up a file into chunks.  From here down.
                foreach (UploadedFile file in RadAsyncUpload1.UploadedFiles)
                    byte[] bytes = new byte[file.ContentLength];
                    byte[] chunk = new byte[1024 * 1024 * 4];
                    int intChunkLength = chunk.Length;
                    int count = 1;
                    int index = 0;
                    using (var streamFile = file.InputStream)
                        while (index <= bytes.Length)
                        if (index + chunk.Length > (bytes.Length))
                            // Remainder
                            int intChunkUsedSoFar = (count - 1) * chunk.Length;
                            int intRemainder = index - intChunkUsedSoFar;
                            chunk = new byte[chunk.Length];
                            streamFile.Read(chunk, index, intRemainder);
                            // Full size
                            chunk = new byte[intChunkLength];
                            streamFile.Read(chunk, index, intChunkLength);
                        File.WriteAllBytes("c:\\chunks\\chunk" + count.ToString() + ".ary", chunk);
                        count = count + 1;
                        index += chunk.Length;
  2. Peter Milchev
    Peter Milchev avatar
    778 posts

    Posted 26 Oct 2016 Link to this post

    Hello Kevin,

    You might consider useful the following StackOverflow forum threads:
    Peter Milchev
    Telerik by Progress
    Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
Back to Top