最近参考网络资料,学习了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. python3 list列表随机选取一个元素、随机选择一个user-agent

    爬虫时适当更换user-agent可以稍微规避一下代理被封的风险... from random import sample ua = [ 'Mozilla/4.0 (compatible; MSIE ...

  2. vs code使用Git

    做一夜的搬运工:https://www.cnblogs.com/richard1015/p/8336429.html

  3. anaconda 环境新建/删除/拷贝 jupyter notebook上使用python虚拟环境 TensorFlow

    naconda修改国内镜像源 国外网络有时太慢,可以通过配置把下载源改为国内的通过 conda config 命令生成配置文件,这里使用清华的镜像: https://mirrors.tuna.tsin ...

  4. Python之爬虫的理解

    #  -*- coding: utf-8 -*-  中文用户一定先用这行来声明编码方式 爬虫: 爬虫是自动访问互联网,并且提取数据的程序  (从网络上获取非结构化的数据,ETL将这些数据转换为结构化数 ...

  5. 解释型语言VS编译型语言

    前言 计算机不能直接理解除机器语言以外的语言,所以只有把程序员编写的程序翻译成机器语言,计算机才能够执行程序. 将其他语言翻译成机器语言的工具,被称之为:编译器. 编译器的翻译方式有两种:编译和解释. ...

  6. C#如何使SQLite程序集既能适应32位系统也能适应64位系统

    分享5: 需求:都知道Sqlite3是分32位和64位版本的,那如果将一个Sqlite3.dll文件全适用 分析:Sqlite是种轻量级的数据库文件,使用了混合编程而成的,一部分采用非托管的C++代码 ...

  7. 树 相关知识总结以及Java实现

    最近在温习树相关的知识,并且用java实现了一下树的遍历相关,先贴上代码供大家参考吧. package tree_problems; import java.util.ArrayDeque; impo ...

  8. 关系型数据库 VS 非关系型数据库

    一.关系型数据库? 1.概念 关系型数据库是指采用了关系模型来组织数据的数据库.简单来说,关系模式就是二维表格模型. 主要代表:SQL Server,Oracle,Mysql,PostgreSQL. ...

  9. 【UOJ448】【集训队作业2018】人类的本质 min_25筛

    题目大意 给你 \(n,m\),求 \[ \sum_{i=1}^n\sum_{x_1,x_2,\ldots,x_m=1}^i\operatorname{lcm}(\gcd(i,x_1),\gcd(i, ...

  10. C# 将前端传来的图片文件分别以大图和缩略图保存

    HttpPostedFile pic_upload = Request.Files["file"]; Bitmap bitmap = (Bitmap)System.Drawing. ...