1

I am currently in the process of updating my companies applications to dotnet 5. One issue I have come up against in several applications is being able to create/generate a file.

The code below is how it was done in dotnet framework.

string output = "a very long string";

Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Length", output.Length.ToString());
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment;filename=test.txt");
Response.Write(output);
Response.End();

I have looked around and I cannot find its equivalent in dotnet core.

smuldr
  • 315
  • 1
  • 12

1 Answers1

1

If you have to return a file in request (assuming you're talking about ASP.NET Core), you'll do:

public IActionResult ReturnAFile()
{
    ...
    // filestream in your question: stream on output
    return File(filestream, "content/type", "output.txt");
}

Maybe this SO Answer can give you some hints.

Kraego
  • 2,978
  • 2
  • 22
  • 34
  • 2
    While that's true, it doesn't do anything like what the OP described as "create a file" in the question. The code basically shows attaching a file in an HTTP response, so I expect the OP wants to know how to do that in ASP.NET Core. – Jon Skeet Sep 28 '21 at 14:27
  • Thanks for the input, never thought that I really ever would communicate with The Jon Skeet ... – Kraego Sep 28 '21 at 15:12