Example #1
0
        /// <summary>
        /// 上传文件带进度,进度委托(最大值,进度值,进度百分比)
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="Fields">字段集合,不可null</param>
        /// <param name="Files">文件集合,不可null</param>
        /// <param name="progressAction">进度委托,不可null</param>
        /// <param name="token">取消Token</param>
        /// <returns></returns>
        public Task <HttpResponseMessage> ExPostFormDataAsync(string url, Dictionary <string, string> Fields, Dictionary <string, string> Files, Action <string, string, int> progressAction, CancellationToken token = default)
        {
            void HttpSendProgress(object sender, HttpProgressEventArgs e) => progressAction?.Invoke(ExCommon.HumanReadableFilesize(e.TotalBytes), ExCommon.HumanReadableFilesize(e.BytesTransferred), e.ProgressPercentage);

            processMessageHander.HttpSendProgress += HttpSendProgress;
            MultipartFormDataContent formdata = new MultipartFormDataContent();

            foreach (var field in Fields)
            {
                formdata.Add(new StringContent(field.Value), field.Key);
            }
            var files = Files?.Select(x => new { Key = x.Key, FileName = x.Value, StreamVal = File.OpenRead(x.Value) }).ToList();

            files.ForEach(x => formdata.Add(new StreamContent(x.StreamVal), x.Key, Path.GetFileName(x.FileName)));
            return(PostAsync(url, formdata, token).ContinueWith(x => {
                processMessageHander.HttpSendProgress -= HttpSendProgress;
                files.ForEach(x => x.StreamVal.Close());
                return x.GetAwaiter().GetResult();
            }));
        }
Example #2
0
        /// <summary>
        /// 下载网址到byte[],带进度,进度委托(最大值,进度值,进度百分比)
        /// </summary>
        /// <param name="address">请求地址</param>
        /// <param name="progressAction">进度委托</param>
        /// <param name="token">取消Token</param>
        /// <returns></returns>
        public Task <byte[]> ExDownloadBytesAsync(string address, Action <string, string, int> progressAction, CancellationToken token = default)
        {
            void HttpReceiveProgress(object sender, HttpProgressEventArgs e) => progressAction?.Invoke(ExCommon.HumanReadableFilesize(e.TotalBytes), ExCommon.HumanReadableFilesize(e.BytesTransferred), e.ProgressPercentage);

            processMessageHander.HttpReceiveProgress += HttpReceiveProgress;
            return(GetByteArrayAsync(address, token).ContinueWith(x => {
                processMessageHander.HttpReceiveProgress -= HttpReceiveProgress;
                return x.GetAwaiter().GetResult();
            }));
        }
        /// <summary>
        /// 下载网址包括本地文件到指定路径,主要用于单个本地文件显示拷贝进度,进度委托(最大值,进度值,进度百分比)
        /// </summary>
        /// <param name="address">请求地址</param>
        /// <param name="saveFileName">保存文件全路径</param>
        /// <param name="progressValueAction">进度委托,最大值100</param>
        /// <returns></returns>
        public static Task ExDownFileAsync(this WebClient webClient, string address, string saveFileName, Action <string, string, int> progressAction)
        {
            void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) => progressAction?.Invoke(ExCommon.HumanReadableFilesize(e.TotalBytesToReceive), ExCommon.HumanReadableFilesize(e.BytesReceived), e.ProgressPercentage);

            webClient.DownloadProgressChanged += DownloadProgressChanged;
            return(webClient.DownloadFileTaskAsync(address, saveFileName).ContinueWith(x => {
                webClient.DownloadProgressChanged -= DownloadProgressChanged;
                x.GetAwaiter().GetResult();
            }));
        }