'Excel gets Corrupted When File Uploaded is Uploaded in Azure Blob Storage in Chunks

** Scenario: I have uploaded a 50MB Excel File through below method creates chunks of the file of 10MB. Two Chunks were uploaded successfully and while uploading third there was some network issue and upload stops. When resume the upload first two chunks that were completed successfully are skipped and remaining chunks are created and uploaded. Using PutBlockListAsync I have combined all the blockids.

While downloading the file from azure storage Explorer. File is corrupt error dialog comes on the screen. Where am I going on? What fix can I add for this?Excel Error Message**

private async Task UploadFilesAsync(CloudBlockBlob blob, IFormFile file)
    {

        try
        {
            //List of block ids; the blocks will be committed in the order of this list 
            HashSet<string> blocklist = new HashSet<string>();
            const int pageSizeInBytes = 10 * 1024 * 1024; // 10MB

            long bytesRead = 0; //number of bytes read so far
            long bytesRemain = file.Length; //number of bytes left to read and upload

            int blocksUploaded = 0;
            byte[] bytes;
           //Previous completed block Id
            blocklist.Add("QmxvY2tJZDAwMDAwMDE=");
            blocklist.Add("QmxvY2tJZDAwMDAwMDI=");
            int completedBlocks = 2;

            using (MemoryStream ms = new MemoryStream())
            {
                var fileStream = file.OpenReadStream();
                await fileStream.CopyToAsync(ms);
                bytes = ms.ToArray();
            }

            // Upload each piece
            do
            {
                //get no of bytes remaining to upload 
                long bytesToCopy = Math.Min(bytesRemain, pageSizeInBytes);
                
                //set up new buffer with the right size, and read that many bytes into it 
                byte[] bytesToSend = new byte[bytesToCopy];

                //copy bytesToSend data from bytes array
                Array.Copy(bytes, bytesRead, bytesToSend, 0, bytesToCopy);

                //increment/decrement counters
                bytesRead += bytesToCopy;
                bytesRemain -= bytesToCopy;
                blocksUploaded++;

                if (blocksUploaded > completedBlocks) 
                {
                    //create a blockID from the block number, add it to the block ID list
                    //the block ID is a base64 string
                    string blockId =
                          Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("BlockId{0}",
                            blocksUploaded.ToString("0000000"))));

                    //upload the block, provide the hash so Azure can verify it
                    await blob.PutBlockAsync(
                        blockId,
                        new MemoryStream(bytesToSend, true),
                        null
                        );

                    blocklist.Add(blockId);
                }
                
            } while (bytesRemain > 0);

            //commit the blocks
            await blob.PutBlockListAsync(blocklist);

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source