Asp.net mvc FileStreamResult

The first part of the question:

I have information in the database and I want to get it from db and save it as a .txt file for the client.

I did this using regular asp.net. but in mvc not yet. My information is not an image. This information about peoples

I watched this site

The second part of the question:

I want to upload a file to the client. There is no problem downloading one file, but I want to download 3 files at the same time as 1 request. But this is unreal. So I decided to create a zip file and create a link. When the user clicks the link, it will be loaded to the user.

What do you think? Is it good to do it this way?

Third part of the question: (new)

How can I delete old .zip files from the directory after succes downloads? or another way. Assume that the service will run on a server.

+6
asp.net-mvc
source share
2 answers

You may have the following controller action that will retrieve information from the database and write it to the Response stream, allowing the client to load it:

 public ActionResult Download() { string info = Repository.GetInfoFromDatabase(); byte[] data = Encoding.UTF8.GetBytes(info); return File(data, "text/plain", "foo.txt"); } 

and in your view, provide a link to this action:

 <%= Html.ActionLink("Downoad file", "Download") %> 
+12
source share

You can delete the temporary file returned with an action filter like this. Then apply the attribute to your MVC action method.

  public class DeleteTempFileResultFilter : ActionFilterAttribute { public override void OnResultExecuted(ResultExecutedContext filterContext) { string fileName = ((FilePathResult)filterContext.Result).FileName; filterContext.HttpContext.Response.Flush(); filterContext.HttpContext.Response.End(); System.IO.File.Delete(fileName); } } 
+3
source share

All Articles