Beispiel #1
0
        public async Task <Stream> DownloadAsync(FilesDownloadModel input, CancellationToken token = default)
        {
            if (string.IsNullOrEmpty(input.FileName) || !(input.Files?.Count > 0))
            {
                throw new ArgumentException("参数无效");
            }

            var response = await _httpClient.PostAsync(FileStorageOption.DownloadUrl,
                                                       new StringContent(JsonConvert.SerializeObject(input),
                                                                         Encoding.UTF8,
                                                                         "application/json"),
                                                       token);

            if (response.IsSuccessStatusCode)
            {
                return(await response.Content.ReadAsStreamAsync());
            }
            throw new HttpRequestException($"下载文件失败,{response.StatusCode}: {await response.Content.ReadAsStringAsync()}");
        }
Beispiel #2
0
 public static async Task DownloadToFileAsync(this IFileStorage storage, string output, FilesDownloadModel input,
                                              CancellationToken token = default)
 {
     using (var stream = await storage.DownloadAsync(input, token))
     {
         using (var writer = File.Open(output, FileMode.OpenOrCreate, FileAccess.ReadWrite))
         {
             writer.SetLength(0);
             writer.Seek(0, SeekOrigin.Begin);
             await stream.CopyToAsync(writer, 4096, token);
         }
     }
 }