Issue with Azure chunked upload to fileshare via Azure.Storage.Files.Shares library

前端 未结 1 1574
悲哀的现实
悲哀的现实 2020-12-22 03:27

I\'m trying to upload files to an Azure fileshare using the library Azure.Storage.Files.Shares.

If I don\'t chunk the file (by making a single UploadRange call) it w

相关标签:
1条回答
  • 2020-12-22 03:33

    I was able to reproduce the issue. Basically the problem is with the following line of code:

    new HttpRange(0, uploadChunk.Length)
    

    Essentially you're always setting the content at the same range and that's why the file is getting corrupted.

    Please try the code below. It should work. What I did here is defined the HTTP range offset and moving it constantly with number of bytes already written to the file.

            using (FileStream stream = fileInfo.OpenRead())
            {
                file.Create(stream.Length);
    
                //file.UploadRange(new HttpRange(0, stream.Length), stream);
    
                int blockSize = 1 * 1024;
                long offset = 0;//Define http range offset
                BinaryReader reader = new BinaryReader(stream);
                while (true)
                {
                    byte[] buffer = reader.ReadBytes(blockSize);
                    if (buffer.Length == 0)
                        break;
    
                    MemoryStream uploadChunk = new MemoryStream();
                    uploadChunk.Write(buffer, 0, buffer.Length);
                    uploadChunk.Position = 0;
    
                    HttpRange httpRange = new HttpRange(offset, buffer.Length);
                    var resp = file.UploadRange(httpRange, uploadChunk);
                    offset += buffer.Length;//Shift the offset by number of bytes already written
                }
    
                reader.Close();
            }
    
    0 讨论(0)
提交回复
热议问题