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 ...
随机推荐
- eclipse java formater 配置详解
comment.insert_new_line_before_root_tags(insert/do_not_insert):在Javadoc根标记块前插入空行,默认为insert: insert_s ...
- GDB调试指南-变量查看
前言 在启动调试以及设置断点之后,就到了我们非常关键的一步-查看变量.GDB调试最大的目的之一就是走查代码,查看运行结果是否符合预期.既然如此,我们就不得不了解一些查看各种类型变量的方法,以帮助我们进 ...
- Java注解原理
1. @interface不是接口是注解类,使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节 2. @in ...
- bzoj2006 [NOI2010]超级钢琴 (及其拓展)
bzoj2006 [NOI2010]超级钢琴 给定一个序列,求长度在 \([L,\ R]\) 之间的区间和的前 \(k\) 大之和 \(n\leq5\times10^5,\ k\leq2\times1 ...
- 【原创】小说:我是一条DQL
SQL执行流程图如下 本文改编自<高性能Mysql>,烟哥用小说的形式来讲这个内容. 序章 自我介绍 我是一条sql,就是一条长长的字符串,不要问我长什么样,因为我比较傲娇. 额~~不是我 ...
- ASP.NET Core 2.2 十九. Action参数的映射与模型绑定
前文说道了Action的激活,这里有个关键的操作就是Action参数的映射与模型绑定,这里即涉及到简单的string.int等类型,也包含Json等复杂类型,本文详细分享一下这一过程.(ASP.NET ...
- 利用ELK分析Nginx日志生产实战(高清多图)
本文以api.mingongge.com.cn域名为测试对象进行统计,日志为crm.mingongge.com.cn和risk.mingongge.com.cn请求之和(此二者域名不具生产换环境统计意 ...
- 在JavaScript中使用三目运算符时进行多个操作
今天使用三目运算符时,刚好需要在false时进行两个操作,故测试并记录在三目运算符中使用多个操作的方式 例子如下: true ? (console.log(1),console.log(2), tes ...
- 一次jdk1.7升级jdk1.8后导致redis运行时blocked_clients过多问题解决
公司有个采集项目,因为请求量较大,添加了redis集群,并且升级了原有的jdk1.7到jdk1.8版本,之后问题就出来了. 1.程序运行一段时间就自动停止,必须重启才能再次运行. 2.redis连接监 ...
- Python基础:编码规范(4)
1.命名规范 Python中不同代码元素采用不同命名方式: ◊ 包名:全部小写字母,中间可以由点分隔开.作为命名空间,包名需具有唯一性. ◊ 模块名:全部小写字母,如果是多个单词构成,使用下划线分隔. ...