This is a migrated thread and some comments may be shown as answers.

FileUploaded - InputStream issue

1 Answer 347 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Kevin J
Top achievements
Rank 1
Kevin J asked on 21 Oct 2016, 07:28 PM

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);
                         
                    }
                    else
                    {
                        // 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;
 
                }
            }
}}

1 Answer, 1 is accepted

Sort by
0
Peter Milchev
Telerik team
answered on 26 Oct 2016, 02:12 PM
Hello Kevin,

You might consider useful the following StackOverflow forum threads:
Regards,
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.
Tags
AsyncUpload
Asked by
Kevin J
Top achievements
Rank 1
Answers by
Peter Milchev
Telerik team
Share this question
or