ASP.NET MVC上传文件
最近参考网络资料,学习了ASP.NET MVC如何上传文件。最基本的,没有用jQuery等技术。
1、定义Model
public class TestModel
{
[Display(Name = "标题")]
[Required]
public string Title
{
get;
set;
}
[Display(Name = "内容")]
[Required]
[DataType(DataType.MultilineText)]
public string Content
{
get;
set;
}
public string AttachmentPath
{
get;
set;
}
}
2、Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class TestController : Controller { // // GET: /Test/ public ActionResult Upload() { return View(); } /// <summary> /// 提交方法 /// </summary> /// <param name="tm">模型数据</param> /// <param name="file">上传的文件对象,此处的参数名称要与View中的上传标签名称相同</param> /// <returns></returns> [HttpPost] public ActionResult Upload(TestModel tm, HttpPostedFileBase file) { if (file == null ) { return Content( "没有文件!" , "text/plain" ); } var fileName = Path.Combine(Request.MapPath( "~/Upload" ), Path.GetFileName(file.FileName)); try { file.SaveAs(fileName); //tm.AttachmentPath = fileName;//得到全部model信息 tm.AttachmentPath = "../upload/" + Path.GetFileName(file.FileName); //return Content("上传成功!", "text/plain"); return RedirectToAction( "Show" ,tm); } catch { return Content( "上传异常 !" , "text/plain" ); } } public ActionResult Show(TestModel tm) { return View(tm); } } |
3、View
3.1 Upload.cshtml
@model MvcApplication1.Models.TestModel @{ ViewBag.Title = "Upload" ; } <!DOCTYPE html> <html> <head> <title>普通上传</title> </head> <body> @*enctype= "multipart/form-data" 是必需有的,否则action接收不到相应的file*@ @ using (Html.BeginForm( "Upload" , "Test" , FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.LabelFor(mod => mod.Title) <br /> @Html.EditorFor(mod => mod.Title) <br /> <br /> @Html.LabelFor(mod => mod.Content) <br /> @Html.EditorFor(mod => mod.Content) <br /> <span>上传文件</span> <br /> <input type= "file" name= "file" /> <br /> <br /> <input id= "ButtonUpload" type= "submit" value= "提交" /> } </body> </html> |
3.2 Show.cshtml
@model MvcApplication1.Models.TestModel @{ ViewBag.Title = "Show" ; } <h2>Show</h2> @Html.LabelFor(mod => mod.Title) <br /> @Html.EditorFor(mod => mod.Title) <br /> <br /> @Html.LabelFor(mod => mod.Content) <br /> @Html.EditorFor(mod => Model.Content) <br /> 图片: <img src= "@Model.AttachmentPath" alt= "img" /> |
ASP.NET MVC上传文件的更多相关文章
- asp.net MVC 上传文件 System.Web.HttpException: 超过了最大请求长度
APS.NET MVC 上传文件出现 System.Web.HttpException: 超过了最大请求长度 这个问题 原因是 默认最大上传文件大小为4096,而我提交的文件太大了. 解决方案:修改 ...
- ASP.NET MVC上传文件----uploadify的使用
课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:uploadify. upl ...
- asp.net mvc 上传文件
转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...
- ASP.NET MVC上传文件 未显示页面,因为请求实体过大。解方案
在Dropzone中设置 maxFilesize: 350, //MB 但上传的文件没有到最大限定350MB,就报出来 未显示页面,因为请求实体过大的错误 Web.config中设置 maxAl ...
- IIS ASP.NET MVC 上传文件到NAS目录
项目要求,网站用户上传的文件,存储到服务器挂接的NAS磁盘里,死活也写不进去,一直提示 System.IO.IOException: 指定的服务器无法运行请求的操作 阿里的客服也问过了, 一群只知道发 ...
- ASP.NET MVC上传文件的几种方法
1.Form表单提交 <p>Form提交</p> <form action="@Url.Action("SavePictureByForm" ...
- MVC上传文件
ASP.NET MVC上传文件是必段撑握的知识.加强训练才是.以前Insus.NET曾使用第三方MyAjaxForm.js :http://www.cnblogs.com/insus/p/378548 ...
- asp.net mvc上传头像加剪裁功能
原文:asp.net mvc上传头像加剪裁功能 正好项目用到上传+剪裁功能,发上来便于以后使用. 我不能告诉你们其实是从博客园扒的前台代码,哈哈. 前端是jquery+fineuploader+jqu ...
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
随机推荐
- 【内容】MVP 三剑客活动
最近微软搞了一个活动,叫做三剑客,主旨就是“Cloud+AI本地化社区活动,为微软产品本地化做出自己的贡献”,虽然已是rMVP,但也同样收到的社区经理的来信,本人也报名参加了这个活动,同时给了我三个小 ...
- 关于C#中的++运算符的一些拓展思考
在刷LeetCode题库的时候,看到一个大神写的for循环是这样的 ;i<length;++i) { //dosomething } 其实最终的效果和 ;i<l;i++){} 是一样的. ...
- #034Python选修课第二届Turtle绘图大赛
Pythonturtle库选修课作业 目录 目录 代码效果 题目要求 合作同学 程序实现 最初目标 实现方式 代码如下 代码效果 题目要求 具体内容可参阅课程教学网站或超星学习通. 按照2人一组,结对 ...
- git tag 打标签
注意:在哪个分支上打tag一定要先提交该分支到远程gitlab仓库 标签(tag)操作 1. 查看所有标签 git tag 默认标签是打在最新提交的commit上的 2.本地打新标签 git tag ...
- 网络流 E - Escape HDU - 3605
2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...
- ARTS打卡第四周
Algorithm 只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用 ...
- android获取string.xml的值
在android开发过程中,编写java代码中的常量过一般情况下,我们是定义在string.xml这个文件中.这样修改起来也很方便,而且做国际化也很简单. 这个string.xml的值会被R文件映射, ...
- Java多线程面试
1.说说进程.线程.协程之间的区别 简而言之,进程是程序运行和资源分配的基本单位,一个程序至少有一个进程,一个进程至少有一个线程.进程在执行过程中拥有独立的内存单元,而多个线程共享内存资源,减少切换次 ...
- 程序员如何避免996、icu?欢迎来讨论一下。
最近996icu火了,我以前就被996害了.现在还没缓过来,可能是自己体质比较弱吧. 以前的事就不说了,说说现在的想法吧.那么程序员如何才能避免996icu呢? 有两个基本因素: 1. 实现一个功能, ...
- java集合-HashMap源码解析
HashMap 键值对集合 实现原理: HashMap 是基于数组 + 链表实现的. 通过hash值计算 数组索引,将键值对存到该数组中. 如果多个元素hash值相同,通过链表关联,再头部插入新添加的 ...