□ 思路

点击一个链接,把该文件的Id传递给控制器方法,遍历文件夹所有文件,根据ID找到对应文件,并返回FileResult类型。

与文件相关的Model:

namespace MvcApplication1.Models
{
public class FileForDownload
{
public int Id { get; set; }
public string Name { get; set; }
public string Path { get; set; }
}
}

写一个针对文件的帮助类,遍历指定文件夹的所有文件,返回FileForDownload集合类型。在项目根目录下创建Files文件夹,存放下载文件。

using System.Collections.Generic;
using System.IO;
using System.Web.Hosting;
using MvcApplication1.Models; namespace MvcApplication1.Helper
{
public class FileHelper
{
public List<FileForDownload> GetFiles()
{
List<FileForDownload> result = new List<FileForDownload>();
DirectoryInfo dirInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/Files")); int i = 0;
foreach (var item in dirInfo.GetFiles())
{
result.Add(new FileForDownload()
{
Id = i + 1,
Name = item.Name,
Path = dirInfo.FullName + @"\" + item.Name
});
i++;
}
return result;
}
}
}

HomeController中:

using System;
using System.Linq;
using System.Web.Mvc;
using MvcApplication1.Helper; namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
private FileHelper helper; public HomeController()
{
helper = new FileHelper();
} public ActionResult Index()
{
var files = helper.GetFiles();
return View(files);
} public FileResult DownloadFile(string id)
{
var fId = Convert.ToInt32(id);
var files = helper.GetFiles();
string fileName = (from f in files
where f.Id == fId
select f.Path).FirstOrDefault();
string contentType = "application/pdf";
return File(fileName, contentType, "Report.pdf");
}
}
}

Home/Index.cshtml中:

@model IEnumerable<MvcApplication1.Models.FileForDownload>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
} <table>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("下载", "DownloadFile", new { id = item.Id })
</td>
</tr> }
</table>

参考资料:
Download Files in ASP.NET MVC 3 using Controller Action

MVC实现文件下载的更多相关文章

  1. MVC 服务器文件下载

    文件上传到服务器后下载 window.open   与window.location.href  对txt  或是pdf文件执行的操作是打开,而非下载 mvc controller 自带有如下方法 p ...

  2. Spring MVC 的文件下载

    在看Spring MVC文件下载之前请先看Spring MVC文件上传 地址:http://www.cnblogs.com/dj-blog/p/7535101.html 文件下载比较简单,在超链接中指 ...

  3. asp.net mvc 简单文件下载

    文件下载,先获取文件的路径,在通过招到文件的存放地址,通过return File(path, "text/plain", Url.Encode(name));,可以直接下载,但是必 ...

  4. Spring MVC实现文件下载

     下载文件① 下载文件需要将byte数组还原成文件. 首先使用mybatis将数据库中的byte数组查出来,指定文件名(包括格式).然后使用OutputStream将文件输入 @RequestMapp ...

  5. C# Mvc中文件下载

    public ActionResult DownloadFile(string id) { var fileinfo = CommonAnnexService.Get(id); if (fileinf ...

  6. mvc之文件下载

    首先你要有四张图片,也就是数组中的数 public ActionResult Index()//创建视图{ViewBag.list =new int[] { 5, 6, 7,8 };return Vi ...

  7. 解决MVC模式文件下载附件中文名称乱码

    解决如下: 进行url编码:Server.UrlPathEncode(file.AttachmentName) return File(file.TempWorkPath, CommonTools.G ...

  8. C# Net MVC 大文件下载几种方式、支持速度限制、资源占用小

    上一篇我们说到大文件的分片下载.断点续传.秒传,有的博友就想看分片下载,我们也来总结一下下载的几种方式,写的比较片面,大家见谅^_^. 下载方式: 1.html超链接下载: 2.后台下载(四种方法:返 ...

  9. MVC&WebForm对照学习:文件下载

    说完了WebForm和MVC中的文件上传,就不得不说用户从服务器端下载资源了.那么今天就扯扯在WebForm和MVC中是如何实现文件下载的.说起WebForm中的文件上传,codeshark在他的博文 ...

随机推荐

  1. MySQL学习笔记:like和regexp的区别

    一.like关键字 like有两个模式:_和% _:表示单个字符,用来查询定长的数据 select name from table where name like '陈__'; %:表示0个或多个任意 ...

  2. vue-cli中如何集成UEditor 富文本编辑器?

    1.根据后台语言下载对应的editor插件版本 地址:https://ueditor.baidu.com/website/download.html 2.将下载好的资源放在/static/uedito ...

  3. 第一篇CodeIgniter框架的下载及安装

    初次学习Php,网上搜了很多php框架,最后选择了CodeIgniter. 安装环境:php5+mysql6.5+iis7 我的电脑是用来办公写文档用的,win7系统,不想换系统,所以就安装了win7 ...

  4. head命令 tail命令

    head命令 head命令用于显示文件的开头的内容.在默认情况下,head命令显示文件的头10行内容. -n<数字>:指定显示头部内容的行数: -c<字符数>:指定显示头部内容 ...

  5. 《精通Python设计模式》学习结构型之适配器模式

    大名鼎鼎~~ 在兼容老系统和其它系统外调用时,用得着~ class Synthesizer: def __init__(self, name): self.name = name def __str_ ...

  6. bzoj 1212: [HNOI2004]L语言

    思路:字典树+dp, dp[ i ] 表示 前缀到 i 能不能被理解, 如果dp[ i ] 是能被理解的那么, 把i + 1, i + 2 ....  在字典树上走,走到一个单词就转移. ,这样可行的 ...

  7. HBase错误:ERROR: Can't get master address from ZooKeeper; znode data == null 解决办法

    一.问题背景 使用命令 $ hbase shell 进入hbase的shell之后使用create命令创建表时出现错误:ERROR: Can't get master address from Zoo ...

  8. ref:LDAP入门

    ref:https://www.jianshu.com/p/7e4d99f6baaf LDAP入门 首先要先理解什么是LDAP,当时我看了很多解释,也是云里雾里,弄不清楚.在这里给大家稍微捋一捋. 首 ...

  9. 美团针对Redis Rehash机制的探索和实践

    背景 Squirrel(松鼠)是美团技术团队基于Redis Cluster打造的缓存系统.经过不断的迭代研发,目前已形成一整套自动化运维体系,涵盖一键运维集群.细粒度的监控.支持自动扩缩容以及热点Ke ...

  10. 2017 Idea 最简易破解 (无需jar包)(个人整理)

    首先 修改host文件: 文件路径:C:\Windows\System32\drivers\etc\hosts 修改:将“0.0.0.0 account.jetbrains.com”追加到hosts文 ...