Skip to content

Commit 837dad0

Browse files
authored
Update README with CSV export usage and method correction
Added usage instructions for handling large CSV files with ASP.NET Core and corrected method name.
1 parent 4a495ef commit 837dad0

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,26 @@ var myExport = new CsvExport(
8181
);
8282
```
8383

84-
Also, methods `ExportToFile` and `ExportAsMemoryStream` and `ExportToBytes` offer an optional encoding parameter.
84+
Also, methods `ExportToFile` and `WriteToStream` and `ExportToBytes` offer an optional encoding parameter.
85+
86+
### Using with ASP.NET Core:
87+
88+
For big CSV files (megabytes) use `WriteToStreamAsync` and write to `Response.Body` directly. This is very important to save memory usage. Here's a handy example:
89+
90+
```c#
91+
public class CsvExportResult(Csv.CsvExport csv, string fileName) : ActionResult
92+
{
93+
public override Task ExecuteResultAsync(ActionContext ctx)
94+
{
95+
var res = ctx.HttpContext.Response;
96+
res.ContentType = "text/csv";
97+
res.Headers.ContentDisposition = $"attachment; filename=\"{fileName}\"";
98+
return csv.WriteToStreamAsync(res.Body, cancellationToken: ctx.HttpContext.RequestAborted);
99+
}
100+
}
101+
```
102+
103+
Use like this: `return new CsvExportResult(csvExport, "filename.csv");`
85104

86105
### License
87106

0 commit comments

Comments
 (0)