Skip to content

Commit bf9b2fa

Browse files
authored
fix: broken submission attachment download in .NET example (#261)
1 parent 318f70f commit bf9b2fa

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

examples/dotnet/Program.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,27 @@ static async Task SaveSubmissionLocally(string submissionName, FormSubmission su
141141
Console.WriteLine($"\nSubmission saved in folder named '{Path.GetFileName(submissionFolderPath)}'");
142142
}
143143

144-
static Task DownloadAndSaveAttachment(Attachment attachment, string submissionFolderPath)
144+
static async Task DownloadAndSaveAttachment(Attachment attachment, string submissionFolderPath)
145145
{
146146
try
147147
{
148-
return sharedHttpClient
149-
.GetAsync(attachment.downloadLink, HttpCompletionOption.ResponseHeadersRead)
150-
.Result
151-
.EnsureSuccessStatusCode()
152-
.Content
153-
.CopyToAsync(new FileStream(Path.Combine(submissionFolderPath, attachment.name), FileMode.Create, FileAccess.Write, FileShare.None))
154-
.ContinueWith(_ => Console.WriteLine($"Submission attachment '{attachment.name}' has been saved {(attachment.isPotentiallyMalicious ? "(flagged as potentially malicious)" : "")}"));
148+
using var response = await sharedHttpClient.GetAsync(
149+
attachment.downloadLink,
150+
HttpCompletionOption.ResponseHeadersRead
151+
);
152+
153+
response.EnsureSuccessStatusCode();
154+
155+
using var fileStream = new FileStream(
156+
Path.Combine(submissionFolderPath, attachment.name),
157+
FileMode.Create,
158+
FileAccess.Write,
159+
FileShare.None
160+
);
161+
162+
await response.Content.CopyToAsync(fileStream);
163+
164+
Console.WriteLine($"Submission attachment '{attachment.name}' has been saved {(attachment.isPotentiallyMalicious ? "(flagged as potentially malicious)" : "")}");
155165
}
156166
catch (Exception exception)
157167
{

0 commit comments

Comments
 (0)