xam developer
xam developer

Reputation: 1983

Write File to Response

I have a controller action. When the action is fired, a call is made to another URL. That all returns a .txt file. I know because I have the link in an email. WHen I click the link, a .txt file gets downloaded (which is what I want).

I need my app to be a wrapper for that. In an attempt to do this, I have the following:

using (var fileClient = new HttpClient())
{
  var textFile = await fileClient.GetStreamAsync(textFileUrl);
  Response.ClearContent();
  Response.ClearHeaders();

  // Now what?
}              

I'm not sure what to do from here. I'm pretty confident that textFile has the text file I need. I just want to let the user download that file.

Upvotes: 0

Views: 160

Answers (1)

Andrei
Andrei

Reputation: 44580

Returning a file stream

// Specify file name
Response.AppendHeader("content-disposition", "inline; filename=myFile.txt");

// Return file stream. textFile - is your file stream
return new FileStreamResult(textFile, "text/plain");

Redirect to a file

return Redirect("http://example.com/myFile.txt");

Upvotes: 1

Related Questions