Quotidien Shaarli

Tous les liens d'un jour sur une page.

September 6, 2022

Export CSV

@{
string CustomRenderingOfActions(Models.FirstConnectionImport item)
{
return $"<a href='{Model.Subsite}{@Url.Page("ImportIndex", "DownloadFile", new { id = item.Id })}' title='Download'>Download</a>";
}
}

<a href="@Url.Page("CreateDescriptionCp", "DownloadFile", new { attachmentid = attachment.Id})">@attachment.Name</a>

public async Task<FileContentResult> OnGetDownloadFile(int attachmentid)
{
try
{
GetByIdQuery getByIdQuery = new GetByIdQuery { Id = attachmentid };
Attachment attachment = await _mediator.Send(getByIdQuery);
return File(attachment.Content, attachment.ContentType, attachment.Name);
}
catch (Exception)
{
throw;
}
}

public async Task<IActionResult> OnPostDownload2()
{
try
{
var res = await _databaseContext.Forms.Where(f => f.CreatedAt >= Filters.Start && f.CreatedAt <= Filters.End).ToListAsync();
using var ms = new MemoryStream();
using (var sw = new StreamWriter(stream: ms, encoding: new UTF8Encoding(true)))
{
CsvConfiguration csvConfiguration = new(CultureInfo.InvariantCulture)
{
ShouldQuote = args => true
};

                using (var cw = new CsvWriter(sw, csvConfiguration))
                {
                    cw.WriteRecords(res);
                }// The stream gets flushed here.
                return File(ms.ToArray(), "text/csv", $"eBankingSms_Forms_{DateTime.UtcNow.Ticks}.csv");
            }
        }
        catch (Exception e)
        {
            _logger.LogError(e, $"OnPostDownload2");
            throw;
        }
    }