Skip to content

Commit

Permalink
Handle downloading a missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
ADefWebserver committed Aug 4, 2024
1 parent 10f1b63 commit 345583c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,34 +239,43 @@

private async void DownloadAttachment(DTOAttachment paramAttachment)
{
showWaitAttachmentGraphic = true;
StateHasChanged();
try
{
showWaitAttachmentGraphic = true;
StateHasChanged();

var url = new Uri($"{NavigationManager.BaseUri}api/Files/ReturnFile");
var url = new Uri($"{NavigationManager.BaseUri}api/Files/ReturnFile");

var request = new DTOFileParameter();
request.attachmentID = paramAttachment.attachmentID;
request.emailFileName = paramAttachment.fileName;
request.detailId = SelectedTaskDetail.detailId;
request.portalId = -1;
request.taskId = SelectedTask.taskId ?? -1;
request.ticketPassword = SelectedTask.ticketPassword;
var request = new DTOFileParameter();
request.attachmentID = paramAttachment.attachmentID;
request.emailFileName = paramAttachment.fileName;
request.detailId = SelectedTaskDetail.detailId;
request.portalId = -1;
request.taskId = SelectedTask.taskId ?? -1;
request.ticketPassword = SelectedTask.ticketPassword;

var myContent = JsonConvert.SerializeObject(request);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var myContent = JsonConvert.SerializeObject(request);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

var response = await client.PostAsync(url, byteContent);
var fileName = response.Content.Headers.ContentDisposition.FileNameStar;
var response = await client.PostAsync(url, byteContent);
var fileName = response.Content.Headers.ContentDisposition.FileNameStar;

showWaitAttachmentGraphic = false;
StateHasChanged();
showWaitAttachmentGraphic = false;
StateHasChanged();

using (var stream = await response.Content.ReadAsStreamAsync())
using (var stream = await response.Content.ReadAsStreamAsync())
{
using var streamRef = new DotNetStreamReference(stream: stream);
await JSRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
}
}
catch (Exception ex)
{
using var streamRef = new DotNetStreamReference(stream: stream);
await JSRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
Message = ex.GetBaseException().Message;
showWaitAttachmentGraphic = false;
StateHasChanged();
}
}

Expand Down
17 changes: 12 additions & 5 deletions ADefHelpDeskWebApp/Controllers/ExternalApi/FilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,19 @@ public static DTOFile ReturnFileMethod(DTOFileParameter paramDTOFileParameter, s
}
else
{
// Get file contents
var fileContents = System.IO.File.ReadAllBytes(FullPath);
try
{
// Get file contents
var fileContents = System.IO.File.ReadAllBytes(FullPath);

objDTOFile.Buffer = fileContents;
objDTOFile.FileName = objAttachment.OriginalFileName;
return objDTOFile;
objDTOFile.Buffer = fileContents;
objDTOFile.FileName = objAttachment.OriginalFileName;
return objDTOFile;
}
catch (Exception ex)
{
throw new Exception("Get file contents:", ex);
}
}
}
}
Expand Down

0 comments on commit 345583c

Please sign in to comment.