asp.net mvc FileStreamResult [英] asp.net mvc FileStreamResult

查看:148
本文介绍了asp.net mvc FileStreamResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的第一部分:

我在数据库中有信息,我想从数据库中获取信息,并将其另存为.txt文件到客户端.

I have information in DB and I want to get it from db and save it as .txt file to client.

我已经用Regular asp.net完成了它.但尚未在mvc中.我的信息不是图像.有关人民的信息

I have done it with Regular asp.net. but in mvc not yet. My information is not an image. This information about peoples

我观看了本网站

问题的第二部分:

我想将文件下载到客户端.下载一个文件时没有问题,但是我想一次通过1个请求下载3个文件.但这无法完成.因此,我决定创建zip文件并生成链接.当用户单击链接时,它将下载给用户.

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?

问题的第三部分:(新)

下载成功后,如何从目录中删除旧的.zip文件?或其他方式.让我们说一下将在服务器上运行的服务.

How i can delete old .zip files from catalog after succes download? or another way. Lets say with service which will run on server.

推荐答案

您可以执行以下控制器操作,该操作将从数据库中获取信息并将其写入到Response流中,以允许客户端下载该信息:

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") %>

这篇关于asp.net mvc FileStreamResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆