|
3 | 3 |
|
4 | 4 | using System.IO;
|
5 | 5 | using System.Net;
|
| 6 | +using System.Net.Http; |
6 | 7 | using System.Threading;
|
7 | 8 | using System.Threading.Tasks;
|
8 | 9 | using Microsoft.Bot.Builder.Dialogs;
|
@@ -35,21 +36,24 @@ private async Task<DialogTurnResult> PromptUploadStepAsync(WaterfallStepContext
|
35 | 36 |
|
36 | 37 | private async Task<DialogTurnResult> HandleAttachmentStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
|
37 | 38 | {
|
38 |
| - var fileText = string.Empty; |
| 39 | + var fileText = string.Empty; |
| 40 | + using var client = new HttpClient(); |
39 | 41 |
|
40 | 42 | foreach (var file in stepContext.Context.Activity.Attachments)
|
41 | 43 | {
|
42 | 44 | var remoteFileUrl = file.ContentUrl;
|
43 | 45 | var localFileName = Path.Combine(Path.GetTempPath(), file.Name);
|
44 |
| - string fileContent; |
45 |
| - |
46 |
| - using (var webClient = new WebClient()) |
47 |
| - { |
48 |
| - webClient.DownloadFile(remoteFileUrl, localFileName); |
49 |
| - using var reader = new StreamReader(localFileName); |
50 |
| - fileContent = await reader.ReadToEndAsync(); |
51 |
| - } |
52 |
| - |
| 46 | + string fileContent; |
| 47 | + |
| 48 | + using var stream = await client.GetStreamAsync(remoteFileUrl, cancellationToken); |
| 49 | + using (var fs = new FileStream(localFileName, FileMode.Create)) |
| 50 | + { |
| 51 | + await stream.CopyToAsync(fs, cancellationToken); |
| 52 | + } |
| 53 | + |
| 54 | + using var reader = new StreamReader(localFileName); |
| 55 | + fileContent = await reader.ReadToEndAsync(); |
| 56 | + |
53 | 57 | fileText += $"Attachment \"{file.Name}\" has been received.\r\n";
|
54 | 58 | fileText += $"File content: {fileContent}\r\n";
|
55 | 59 | }
|
|
0 commit comments