3

I have a list of objects which I want to download as csv file via Web Api. However when I test this with Swagger I get no csv download. What am I doing wrong? DailyReportContent contains 2 objects.

This is my code so far:

public IActionResult GetDailyReport(DateTime invoiceDate)
{
    var dailyReportContent = new List<DailyReportModel>().....

    // create csv file
    var engine = new FileHelperEngine<DailyReportModel>
    {
        HeaderText = "headers.."
    };

    using (var stream = new MemoryStream())
    using (var streamWriter = new StreamWriter(stream))
    {
        engine.WriteStream(streamWriter, dailyReportContent);

        var result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "DailyReports.csv" };

        return Ok(result);
    }
}

And this is the response in Swagger:

{
  "version": {
    "major": 1,
    "minor": 1,
    "build": -1,
    "revision": -1,
    "majorRevision": -1,
    "minorRevision": -1
  },
  "content": {
    "headers": [
      {
        "key": "Content-Type",
        "value": [
          "text/csv"
        ]
      },
      {
        "key": "Content-Disposition",
        "value": [
          "attachment; filename=DailyReports.csv"
        ]
      }
    ]
  },
  "statusCode": 200,
  "reasonPhrase": "OK",
  "headers": [],
  "requestMessage": null,
  "isSuccessStatusCode": true
}

EDIT: Even if I add this:

 stream.Flush();
 streamWriter.Flush();

 var reader = new StreamReader(stream);
 var readResult = reader.ReadToEnd();

The readResult is empty. So I guess there goes something wrong when writing the stream?

3 Answers 3

1

Fixed it like this:

var csvContent = LoadcsvContent();

var engine = new FileHelperEngine<DailyReportModel>
{
    HeaderText = "......."
};

var stream = new MemoryStream();
TextWriter streamWriter = new StreamWriter(stream);
engine.WriteStream(streamWriter, csvContent);

streamWriter.Flush();
stream.Seek(0, SeekOrigin.Begin);

return File(await stream, "text/csv", $"Report_{invoiceDate:yyyyMMdd}.csv");

Works like a charm now. Swagger output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to

return result;

Returning "Ok( < object > )" will return a response message containing the object.
You have already created your response message and specified all the content.
So I think you are returning an OK response, containing another OK response message.

2 Comments

Nope, doesn't help. gives me exact the same response and no download csv file. :(
What if you call the endpoint with a browser - do you get the file downloaded?
0

In ASP.NetCore we have return File(ret, "application/octet-stream", fileName);

Return a file with the type we needed, in my case for an .msi package I use "application/octet-stream" for you you may either specify the return type as .CSV here in controller level or on the global level in WebApiConfig class for Formatters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.