Reputation: 3257
First part of question:
I have information in DB and I want to get it from db and save it as .txt
file to client.
I have done it with Regular asp.net. but in mvc not yet. My information is not an image. This information about peoples
I watched to This site
Second part of question:
I want to download file to client. There is not problem when downloading one file, but I want to download 3 file at once time with 1 request. But it could not be done. So I decided to create zip file and generate link. When user will click to link it will download to user.
What you think? Is it good to do it with this way?
Third part of question:(new)
How i can delete old .zip files from catalog after succes download? or another way. Lets say with service which will run on server.
Upvotes: 7
Views: 8634
Reputation: 1010
You can delete the temporary file returned by using 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);
}
}
Upvotes: 3
Reputation: 1039538
You could have the following controller action which will fetch the information from the database and write it to the Response stream allowing the client to download 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") %>
Upvotes: 13