最近参考网络资料,学习了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上传文件的更多相关文章

  1. asp.net MVC 上传文件 System.Web.HttpException: 超过了最大请求长度

    APS.NET MVC 上传文件出现  System.Web.HttpException: 超过了最大请求长度 这个问题 原因是 默认最大上传文件大小为4096,而我提交的文件太大了. 解决方案:修改 ...

  2. ASP.NET MVC上传文件----uploadify的使用

    课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:uploadify. upl ...

  3. asp.net mvc 上传文件

    转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...

  4. ASP.NET MVC上传文件 未显示页面,因为请求实体过大。解方案

    在Dropzone中设置   maxFilesize: 350, //MB 但上传的文件没有到最大限定350MB,就报出来 未显示页面,因为请求实体过大的错误 Web.config中设置  maxAl ...

  5. IIS ASP.NET MVC 上传文件到NAS目录

    项目要求,网站用户上传的文件,存储到服务器挂接的NAS磁盘里,死活也写不进去,一直提示 System.IO.IOException: 指定的服务器无法运行请求的操作 阿里的客服也问过了, 一群只知道发 ...

  6. ASP.NET MVC上传文件的几种方法

    1.Form表单提交 <p>Form提交</p> <form action="@Url.Action("SavePictureByForm" ...

  7. MVC上传文件

    ASP.NET MVC上传文件是必段撑握的知识.加强训练才是.以前Insus.NET曾使用第三方MyAjaxForm.js :http://www.cnblogs.com/insus/p/378548 ...

  8. asp.net mvc上传头像加剪裁功能

    原文:asp.net mvc上传头像加剪裁功能 正好项目用到上传+剪裁功能,发上来便于以后使用. 我不能告诉你们其实是从博客园扒的前台代码,哈哈. 前端是jquery+fineuploader+jqu ...

  9. Spring MVC上传文件

    Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...

随机推荐

  1. windows10滑轮bug

    今天我突然发现我一点也忍受不了在UWP应用.wi10窗口.设置等界面无法使用鼠标滑轮了.这个bug已经困扰了我差不多一年了,从买了这台笔记本就开始了.而且这个问题在中间的某一次升级系统后,也修复了,但 ...

  2. Storm入门(二)集群环境安装

    1.集群规划 storm版本的变更:storm0.9.x   storm0.10.x  storm1.x上面这些版本里面storm的核心源码是由Java+clojule组成的.storm2.x后期这个 ...

  3. MacOS 10.13.6 下装xcode 流程

    1.最好先安装brew https://github.com/Homebrew/brew/releases 自动安装脚本 /usr/bin/ruby -e "$(curl -fsSL htt ...

  4. Ceva定理的四种证明方法

    ${\color{Teal} {Ceva定理}}$设$D.E.F$依次为三角形ABC的边$AB.BC.CA$的内点,记 $λ$=(A,B,D),$μ$=(B,C,E),$v$=(C,A,F) 求证:三 ...

  5. XXXX is not in the sudoers file. This incident will be reported解决方法

    假设你用的是Red Hat系列(包括Fedora和CentOS)的Linux系统.当你执行sudo命令时可能会提示“某某用户 is not in the sudoers file.  This inc ...

  6. 【12】Django 中间件

     前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装 ...

  7. Msi中文件替换

    转自https://blog.csdn.net/davidhsing/article/details/9962377 ※说明:目前可以用于MSI编辑的软件很多,但是有些软件在保存时会在MSI文件中写入 ...

  8. 使用linux backtrace打印出错函数堆栈信息

    一般察看函数运行时堆栈的方法是使用GDB(bt命令)之类的外部调试器,但是,有些时候为了分析程序的BUG,(主要针对长时间运行程序的分析),在程序出错时打印出函数的调用堆栈是非常有用的. 在glibc ...

  9. 关于小米4电信4g刷入第三方ROM无信号解决办法

    from: http://www.yuwantb.com/xiaomi4-lineage-os.html   关于小米4电信4g刷入第三方ROM无信号解决办法 下载这个电信4g补丁包. 链接:http ...

  10. beego框架开发投票网站(1) beego基础之运行逻辑

    本文档需结合beego官方文档食用 博主也仅仅是边学边记录,不保证内容的正确性,请当做通俗读物来看待 首先 beego是一个基于go语言的框架 其次 beego是一个mvc框架 框架可以理解为对底层又 ...