Uploading HTTP progress tracking

后端 未结 7 1401
梦谈多话
梦谈多话 2020-12-13 07:55

I\'ve got WPF application I\'m writing that posts files to one of social networks. Upload itself working just fine, but I\'d like to provide some indication of how far along

7条回答
  •  粉色の甜心
    2020-12-13 08:26

    You could use the WebClient's UploadFile to upload file rather than using writing file as a file stream. In order to track the percentage of the data received and uploaded you can use UploadFileAsyn and subscribe to its events.

    In the code bellow I've used UploadFileAsyn to the upload files synchronously, but it need not to be synchronous as far as you don't dispose the instance of the uploader.

    class FileUploader : IDisposable
    {
        private readonly WebClient _client;
        private readonly Uri _address;
        private readonly string _filePath;
        private bool _uploadCompleted;
        private bool _uploadStarted;
        private bool _status;
    
        public FileUploader(string address, string filePath)
        {
            _client = new WebClient();
            _address = new Uri(address);
            _filePath = filePath;
            _client.UploadProgressChanged += FileUploadProgressChanged;
            _client.UploadFileCompleted += FileUploadFileCompleted;
        }
    
        private void FileUploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
        {
            _status = (e.Cancelled || e.Error == null) ? false : true;
            _uploadCompleted = true;
        }
    
        private void FileUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            if(e.ProgressPercentage % 10 == 0)
            {
                //This writes the pecentage data uploaded and downloaded
                Console.WriteLine("Send: {0}, Received: {1}", e.BytesSent, e.BytesReceived);
                //You can have a delegate or a call back to update your UI about the percentage uploaded
                //If you don't have the condition (i.e e.ProgressPercentage % 10 == 0 )for the pecentage of the process 
                //the callback will slow you upload process down
            }
        }
    
        public bool Upload()
        {
    
            if (!_uploadStarted)
            {
                _uploadStarted = true;
                _client.UploadFileAsync(_address, _filePath);
            }
            while (!_uploadCompleted)
            {
                Thread.Sleep(1000);
            }
            return _status;
        }
    
        public void Dispose()
        {
            _client.Dispose();
        }
    }
    

    Client Code:

                using (FileUploader uploader = new FileUploader("http://www.google.com", @"C:\test.txt"))
            {
                uploader.Upload();
            }
    

    You can register a custom callback (may be a delegate) on the FileUploadProgressChanged event handler to update your WPF UI.

    The upload progress changed event get called more often if your callback for the event does any IO then that'll slowdown the download progress. It's best to have infrequent update e.g. the following code update only evey 10% up.

        private int _percentageDownloaded;
    
        private void FileUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage % 10 == 0 && e.ProgressPercentage > _percentageDownloaded)
            {
    
                _percentageDownloaded = e.ProgressPercentage;
                //Any callback instead of printline
                Console.WriteLine("Send: {0} Received: {1}", e.BytesSent, e.BytesReceived);
            }
        }
    

提交回复
热议问题