public async Task <ActionResult> Download(DownloadAttachmentRequest request) { using (var dbContext = _implementationsContainer.GetLobToolsRepository() as LobToolsDbContext) { var attachment = await dbContext.Attachments.FindAsync(request.Id); HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); return(File(attachment.Data, "application/octet-stream", attachment.FileName)); } }
public async Task <ApiResponse <DownloadAttachmentResponse> > DownloadAttachmentAsync(DownloadAttachmentRequest request) { var downloadResponse = new ApiResponse <DownloadAttachmentResponse>(new DownloadAttachmentResponse()) { Body = { LoanId = request.LoanId, EncompassDocument = request.EncompassDocument, DownloadFullPath = request.DownloadFullPath } }; try { var getAttachmentUrl = await PullAttachmentDownloadUrlAsync(new GetAttachmentUrlRequest { AccessToken = request.AccessToken, AttachmentId = request.EncompassDocument.DocumentId, LoanId = request.LoanId }).ConfigureAwait(false); if (!getAttachmentUrl.StatusCode.IsSuccessStatusCode()) { downloadResponse.Body.IsSuccessBit = false; downloadResponse.Body.FailureResponse = getAttachmentUrl.Body.FailureResponse; return(downloadResponse); } var mediarUrl = getAttachmentUrl.Body.SuccessResponse.MediaUrl; _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add(ConstantString.AuthorizationKey, string.Format(ConstantString.AuthorizationValue, request.AccessToken)); var fileResponse = await FileDownloadHelper.DownloadFileAsync(_httpClient, mediarUrl, request.DownloadFullPath).ConfigureAwait(false); downloadResponse.Body.MediaUrl = mediarUrl; downloadResponse.StatusCode = fileResponse.StatusCode; downloadResponse.Headers = fileResponse.Headers.ToDictionary(x => x.Key, x => x.Value.ToString()); if (!fileResponse.StatusCode.IsSuccessStatusCode()) { downloadResponse.Body.IsSuccessBit = false; downloadResponse.Body.FailureResponse = fileResponse.Body.ErrorResponse; } else { downloadResponse.Body.IsSuccessBit = true; } } catch (Exception ex) { downloadResponse.Body.IsSuccessBit = false; var result = JsonConvert.SerializeObject(new { message = JsonConvert.SerializeObject(ex.Message), stackTrace = ex.StackTrace }); downloadResponse.Body.FailureResponse.Details = result; downloadResponse.Body.FailureResponse.ErrorCode = HttpStatusCode.InternalServerError.ToString(); } return(downloadResponse); }