Skip to content

Commit 345583c

Browse files
committed
Handle downloading a missing file
1 parent 10f1b63 commit 345583c

2 files changed

Lines changed: 42 additions & 26 deletions

File tree

ADefHelpDeskWebApp/Components/Pages/TicketControls/TicketEditDetailControl.razor

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -239,34 +239,43 @@
239239

240240
private async void DownloadAttachment(DTOAttachment paramAttachment)
241241
{
242-
showWaitAttachmentGraphic = true;
243-
StateHasChanged();
242+
try
243+
{
244+
showWaitAttachmentGraphic = true;
245+
StateHasChanged();
244246

245-
var url = new Uri($"{NavigationManager.BaseUri}api/Files/ReturnFile");
247+
var url = new Uri($"{NavigationManager.BaseUri}api/Files/ReturnFile");
246248

247-
var request = new DTOFileParameter();
248-
request.attachmentID = paramAttachment.attachmentID;
249-
request.emailFileName = paramAttachment.fileName;
250-
request.detailId = SelectedTaskDetail.detailId;
251-
request.portalId = -1;
252-
request.taskId = SelectedTask.taskId ?? -1;
253-
request.ticketPassword = SelectedTask.ticketPassword;
249+
var request = new DTOFileParameter();
250+
request.attachmentID = paramAttachment.attachmentID;
251+
request.emailFileName = paramAttachment.fileName;
252+
request.detailId = SelectedTaskDetail.detailId;
253+
request.portalId = -1;
254+
request.taskId = SelectedTask.taskId ?? -1;
255+
request.ticketPassword = SelectedTask.ticketPassword;
254256

255-
var myContent = JsonConvert.SerializeObject(request);
256-
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
257-
var byteContent = new ByteArrayContent(buffer);
258-
byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
257+
var myContent = JsonConvert.SerializeObject(request);
258+
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
259+
var byteContent = new ByteArrayContent(buffer);
260+
byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
259261

260-
var response = await client.PostAsync(url, byteContent);
261-
var fileName = response.Content.Headers.ContentDisposition.FileNameStar;
262+
var response = await client.PostAsync(url, byteContent);
263+
var fileName = response.Content.Headers.ContentDisposition.FileNameStar;
262264

263-
showWaitAttachmentGraphic = false;
264-
StateHasChanged();
265+
showWaitAttachmentGraphic = false;
266+
StateHasChanged();
265267

266-
using (var stream = await response.Content.ReadAsStreamAsync())
268+
using (var stream = await response.Content.ReadAsStreamAsync())
269+
{
270+
using var streamRef = new DotNetStreamReference(stream: stream);
271+
await JSRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
272+
}
273+
}
274+
catch (Exception ex)
267275
{
268-
using var streamRef = new DotNetStreamReference(stream: stream);
269-
await JSRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
276+
Message = ex.GetBaseException().Message;
277+
showWaitAttachmentGraphic = false;
278+
StateHasChanged();
270279
}
271280
}
272281

ADefHelpDeskWebApp/Controllers/ExternalApi/FilesController.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,19 @@ public static DTOFile ReturnFileMethod(DTOFileParameter paramDTOFileParameter, s
287287
}
288288
else
289289
{
290-
// Get file contents
291-
var fileContents = System.IO.File.ReadAllBytes(FullPath);
290+
try
291+
{
292+
// Get file contents
293+
var fileContents = System.IO.File.ReadAllBytes(FullPath);
292294

293-
objDTOFile.Buffer = fileContents;
294-
objDTOFile.FileName = objAttachment.OriginalFileName;
295-
return objDTOFile;
295+
objDTOFile.Buffer = fileContents;
296+
objDTOFile.FileName = objAttachment.OriginalFileName;
297+
return objDTOFile;
298+
}
299+
catch (Exception ex)
300+
{
301+
throw new Exception("Get file contents:", ex);
302+
}
296303
}
297304
}
298305
}

0 commit comments

Comments
 (0)