How to track progress of async file upload to azure storage

后端 未结 4 741
我寻月下人不归
我寻月下人不归 2020-12-03 01:56

Is there way to track the file upload progress to an Azure storage container? I am trying to make a console application for uploading data to Azure using C#.

My c

4条回答
  •  被撕碎了的回忆
    2020-12-03 02:44

    How about this.

    public class ObservableFileStream : FileStream
    {
        private Action _callback;
    
        public ObservableFileStream(String fileName, FileMode mode, Action callback) : base(fileName, mode)
        {
            _callback = callback;
        }
    
        public override void Write(byte[] array, int offset, int count)
        {
            _callback?.Invoke(Length);
            base.Write(array, offset, count);
        }
    
        public override int Read(byte[] array, int offset, int count)
        {
            _callback?.Invoke(Position);
            return base.Read(array, offset, count);
        }
    }
    
    public class Test
    {
        private async void Upload(String filePath, CloudBlockBlob blob)
        {
            ObservableFileStream fs = null;
    
            using (fs = new ObservableFileStream(filePath, FileMode.Open, (current) =>
            {
                Console.WriteLine("Uploading " + ((double)current / (double)fs.Length) * 100d);
            }))
            {
                await blob.UploadFromStreamAsync(fs);
            }
        }
    }
    

提交回复
热议问题