1.单个文件上传

HTML写法:form表单中加enctype="multipart/form-data"

 <form aciont="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file" />
<br/>
<input type="submit" name="提交"/>
</form>

HTML写法:

View写法:同样 form表单中加enctype="multipart/form-data"

 @using (Html.BeginForm("Add", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" id="file" name="file" />
<br/>
<input type="submit" value="提交"/>
}

View写法:

后台Controller中的写法:

 [HttpPost]
public ActionResult Add(HttpPostedFileBase file)
{
string path = "/uploadImg/";
if (!Directory.Exists(Server.MapPath(path)))
{
Directory.CreateDirectory(Server.MapPath(path));
}
if (file != null)
{
string filePath = string.Format("{0}/{1}{2}", path, System.Guid.NewGuid(), System.IO.Path.GetExtension(file.FileName));
file.SaveAs(Server.MapPath(filePath));
}
return View();
}

Controller

2.多个文件上传

就是form表单中多个input file

 @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" id="file" name="file1" />
<input type="file" id="file" name="file2" />
<input type="file" id="file" name="file3" />
<input type="file" id="file" name="file4" />
<input type="file" id="file" name="file5" />
<input type="file" id="file" name="file6" />
<br/>
<input type="submit" value="提交"/>
}

View

 public class HomeController:Controller
{
public ActionResult Index()
{
foreach (string upload in Request.Files)
{
if (!Request.Files[upload].HasFile()) continue;
string path = "/uploadImg/";
string filename = Path.GetFileName(Request.Files[upload].FileName);
Request.Files[upload].SaveAs(Path.Combine(path, filename));
}
return View();
}
}

Controller

3.文件下载

HTML中:<a href="/GetFile">下载文件</a>
View中:@Html.ActionLink("下载文件", "Download", "Home");

直接下载到默认下载中

  public class HomeController : Controller
{
public FilePathResult GetFileFromDisk()
{
string path = @"/uploadImg/";
string fileName = "5141fb63-955b-4f0b-9f53-ab770329e6f5.png";
return File(path + fileName, "image/png", fileName);
} public FileStreamResult StreamFileFromDisk()
{
string path = AppDomain.CurrentDomain.BaseDirectory + "uploadImg/";
string fileName = "5141fb63-955b-4f0b-9f53-ab770329e6f5.png";
return File(new FileStream(path + fileName, FileMode.Open), "image/png", fileName);
}
}
}

Controller

MVC文件上传和下载的更多相关文章

  1. MVC文件上传与下载

    MVC文件上传与下载 MVC文件上传与下载 想想自己从毕业到工作也有一年多,以前公司的任务的比较重,项目中有的时候需要用到什么东西都去搜索一下,基础知识感觉还没有以前在学校中的好.最近开始写博客,真的 ...

  2. spring mvc文件上传和下载

    首先要导入2个包(上传文件包和io的包)

  3. 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity

    文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...

  4. Java Web 学习(8) —— Spring MVC 之文件上传与下载

    Spring MVC 之文件上传与下载 上传文件 表单: <form action="upload" enctype="multipart/form-data&qu ...

  5. 使用Spring MVC实现文件上传与下载

    前段时间做毕业设计的时候,想要完成一个上传文件的功能,后来,虽然在自己本地搭建了一个ftp服务器,然后使用公司的工具完成了一个文档管理系统:但是还是没有找到自己想要的文件上传与下载的方式. 今天看到一 ...

  6. 文件上传和下载(可批量上传)——Spring(二)

    针对SpringMVC的文件上传和下载.下载用之前“文件上传和下载——基础(一)”的依然可以,但是上传功能要修改,这是因为springMVC 都为我们封装好成自己的文件对象了,转换的过程就在我们所配置 ...

  7. MVC文件上传-使用jQuery.FileUpload和Backload组件实现文件上传

    本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: 处理文件上传的服务端组件Backload 用于处理文件上传的服务端组件 ...

  8. ASP.NET MVC 文件上传和路径处理

    ASP.NET MVC 文件上传和路径处理总结 目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传 ...

  9. SpringMVC 实现文件上传与下载,并配置异常页面

    目录 上传文件的表单要求 Spring MVC实现上传文件 需要导入的jar包 配置MultipartResolver解析器 编写接收上传文件的控制器 Spring MVC实现文件下载 下载文件时的h ...

随机推荐

  1. ajax data传值

    如果要我们要把整个form1表单中的输入框都传过去可以用data:$("#form1").serialize(), 但是我们如果还要传一个不在form中的值呢? 可以使用下面这种方 ...

  2. We have a problem with promises

    原文地址:http://fex.baidu.com/blog/2015/07/we-have-a-problem-with-promises/ 用Javascript的小伙伴们,是时候承认了,关于 p ...

  3. Java类额应用

    基本数据类型包装类  Integer    Character  其他的都是将首字母大写;   包装类和基本类型之间的转换:  Integer   int Integer i = new  Integ ...

  4. 事件:卸载事件(onunload)

    这是几点应卸载事件的说明 ①目前试了Firefox.Google Chrome.IE三个浏览器,该事件只对IE起作用. ②onunload事件对于刷新页面和超链接跳转其他页面情况有效,对于关闭页面无效 ...

  5. Apache伪静态在网站目录没有反斜杠后自动添加反斜杠

    第一步:确认网站开启REWRITE规则 一般有两种情况: i.apache安装的时候已经包含rewrite功能 ii.后续配置的时候新添加mod_rewrite.so.这种情况需要在httpd.con ...

  6. lua 代码风格

    参考  http://www.kancloud.cn/kancloud/lua_style_guide/66327 1.命名 1.命名法:小驼峰命名法,大驼峰命名法(Pascal命名法),小下划线命名 ...

  7. PLSQL win7 64位

    1. 解压instantclient-basic-win32-11.2.0.1.0.zip至Oracle安装目录的Product下 具体目录如下D:\Oracle\product\instantcli ...

  8. linux内核升级图文攻略(转)

    一.Linux内核概览Linux是一个一体化内核(monolithic kernel)系统.设备驱动程序可以完全访问硬件.Linux内的设备驱动程序可以方便地以模块化(modularize)的形式设置 ...

  9. [整理]Matlab之中心平滑滤波

    滑动平均(moving average):在地球物理异常图上,选定某一尺寸的窗口,将窗口内的所有异常值做算术平均,将平均值作为窗口中心点的异常值.按点距或线距移动窗口,重复此平均方法,直到对整幅图完成 ...

  10. 学习 OPenGL

    今天在网上看到一篇硕士论文<基于OpenGL三维虚拟场景建模技术研究_王志杰>,学习到OpenGL可以进行三维重建,决定从现在开始学习OpenGL,特开此贴.