public async Task <IActionResult> Export([FromBody] ExportVm vm) { if (vm == null) { return(BadRequest()); } int userId; try { userId = IdentityHelper.GetUserId(User); } catch (UnauthorizedAccessException) { return(Unauthorized()); } string directory = Path.Combine(_webHostEnvironment.ContentRootPath, "storage", "temp"); var exportAsCsvModel = new ExportAsCsv(userId, directory, vm.FileId, _localizer["Uncategorized"], _localizer["Encrypted"]); FileStream file = await _transactionService.ExportAsCsvAsync(exportAsCsvModel); Response.Headers.Add("Content-Disposition", "attachment; filename=\"transactions.csv\""); return(new FileStreamResult(file, "text/csv")); }
public async Task <FileStream> ExportAsCsvAsync(ExportAsCsv model) { string fileName = model.FileId + ".csv"; string tempFilePath = Path.Combine(model.Directory, fileName); IEnumerable <Transaction> transactions = await _transactionsRepository.GetAllForExportAsync(model.UserId, model.Uncategorized); foreach (var transaction in transactions) { if (transaction.IsEncrypted) { transaction.Description = model.Encrypted; } } using (var writer = new StreamWriter(tempFilePath)) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap <TransactionMap>(); csv.WriteRecords(transactions); } return(new FileStream(tempFilePath, FileMode.Open)); }