当前位置: 首页>>代码示例>>C#>>正文


C# Progress.?.CompletedDownload方法代码示例

本文整理汇总了C#中System.Progress.?.CompletedDownload方法的典型用法代码示例。如果您正苦于以下问题:C# Progress.?.CompletedDownload方法的具体用法?C# Progress.?.CompletedDownload怎么用?C# Progress.?.CompletedDownload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Progress的用法示例。


在下文中一共展示了Progress.?.CompletedDownload方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FetchAsync

        public async Task<TempPackage> FetchAsync(Package pkg, Progress progress)
        {
            var user = pkg.Location.Host;
            var project = pkg.Location.Segments.Skip(1).SingleOrDefault()?.Trim('/');
            var props = pkg.Location.ParseQueryString();
            var url = $"https://api.github.com/repos/{user}/{project}/releases/latest";

            using (var httpClient = NetUtils.HttpClient())
            {
                var content = await httpClient.GetStringAsync(url);

                var json = fastJSON.JSON.Parse(content) as IDictionary<string, object>;
                var assets = json["assets"] as IList<object>;
                var asset = NarrowAssets(assets, props);
                var pkgUrlString = asset["browser_download_url"] as string;
                Uri pkgUri;
                if (!Uri.TryCreate(pkgUrlString, UriKind.Absolute, out pkgUri))
                {
                    throw new UriFormatException($"Could not parse output from Github API, failed to parse URI: '{pkgUrlString}'");
                }
                var result = new TempPackage
                {
                    Package = pkg,
                    WorkDirectory = new TempDirectory("winston"),
                    FileName = pkgUri.LastSegment()
                };

                using (var download = File.Open(result.FullPath, FileMode.Create, FileAccess.ReadWrite))
                {
                    await httpClient.DownloadFileAsync(pkgUri, download, progress);
                }
                progress?.CompletedDownload();
                return result;
            }
        }
开发者ID:mattolenik,项目名称:winston,代码行数:35,代码来源:GithubFetcher.cs

示例2: FetchAsync

        public async Task<TempPackage> FetchAsync(Package pkg, Progress progress)
        {
            var result = new TempPackage { WorkDirectory = new TempDirectory("winston") };
            using (var handler = new HttpClientHandler { AllowAutoRedirect = true })
            using (var client = NetUtils.HttpClient(handler))
            using (var response = await client.GetAsync(pkg.Location, HttpCompletionOption.ResponseHeadersRead))
            {
                result.MimeType = response.Content.Headers.ContentType?.MediaType;

                var uriFileName = pkg.Location.LastSegment();
                // Heuristic: assume the last path segment is a file name if it contains a dot for the file extension
                if (!uriFileName.Contains('.'))
                {
                    uriFileName = null;
                }
                // Try to get the right file name to give hints about the file type to the extractor
                result.FileName = response.Content.Headers.ContentDisposition?.FileName?.Trim('\"', '\\', '\'') ??
                                  uriFileName ??
                                  "package";

                var total = response.Content.Headers.ContentLength ?? -1L;

                using (var stream = await response.Content.ReadAsStreamAsync())
                using (var output = File.Open(result.FullPath, FileMode.Create, FileAccess.ReadWrite))
                {
                    var totalRead = 0L;
                    var buffer = new byte[81920]; // Default size from .NET docs on CopyTo
                    while (true)
                    {
                        var read = await stream.ReadAsync(buffer, 0, buffer.Length);
                        if (read == 0)
                        {
                            break;
                        }
                        await output.WriteAsync(buffer, 0, read);
                        totalRead += read;
                        // Can't report progress if there was no Content-Length
                        if (total > 0)
                        {
                            progress?.Report(totalRead * 1d / (total * 1d) * 100);
                        }
                    }
                    progress?.Report(100);
                    progress?.CompletedDownload();
                }
            }

            var hash = await FileSystem.GetSha1Async(result.FullPath);
            // Only check when Sha1 is specified in the package metadata
            if (!string.IsNullOrWhiteSpace(pkg.Sha1) &&
                !string.Equals(hash, pkg.Sha1, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidDataException($"SHA1 hash of remote file {pkg.Location} did not match {pkg.Sha1}");
            }
            return result;
        }
开发者ID:mattolenik,项目名称:winston,代码行数:56,代码来源:HttpFetcher.cs


注:本文中的System.Progress.?.CompletedDownload方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。