- Controllers:
- using Mvc7._1.Models;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.web;
- using System.Web.Mvc;
- namespace Mvc7._1.Controllers
- {
- public class HomeController : Controller
- {
- //
- // GET: /Home/
- public ActionResult Index()
- { return View();
- }
- public string test(string name, int? age)// 使用模型绑定获取基本类型数据
- { // 获取基本类型 string int int 后的? 表示可空类型 如不加上? 输入类型不符将会报错
- return name + "\t" + age;
- }
- [HttpPost] // 只处理 post 提交的 不能处理 get 提交
- public ActionResult add(info info)// 使用默认模型绑定获取模型对象数据 页面 name 的字段和实体对象名称要一致
- {
- string msg = null;
- if (info.file.ContentLength> 0)
- {
- info.file.SaveAs(Server.MapPath("~/Content/upload/" + info.file.FileName));
- //IE 中报 System.NotSupportedException:"不支持给定路径的格式."
- // 这种方式在 ie 中不兼容所以要用 Path.GetFileName(); 转换
- msg = "上传成功!";
- }
- else
- {
- msg = "未选择文件";
- }
- ViewBag.msg = info.name + "\t" + info.pwd + "\t" + info.sex + "\t" + info.age + "\t" + msg;
- return View("index");// 调用 index 页面
- // 页面上需要输出 <div>
- //<p style="color:red">@ViewBag.msg</p>
- //</div>
- }
- // 可处理 post 或 get 请求 但是有文件上传时候用到 Request.Files[0] 的时候必须使用 post 方式提交
- public string add()
- {
- info info = new info();
- info.name = Request["name"];
- info.age = int.Parse(Request["age"]);
- info.sex = Request["sex"];
- info.pwd = Request["pwd"];
- info.file = Request.Files[0];// 提交方式是 post 才可以 get 方式获取不到值
- string msg = null;
- if (info.file.ContentLength> 0)
- {
- //info.file.SaveAs(Server.MapPath("~/Content/upload/") + info.file.FileName);
- string FileName = Path.GetFileName(info.file.FileName);
- info.file.SaveAs(Server.MapPath("~/Content/upload/") + FileName);
- msg = "上传成功!";
- }
- else
- {
- msg = "未选择文件";
- }
- return info.name + "\t" + info.pwd + "\t" + info.sex + "\t" + info.age + "\t" + msg;
- }
- }
- };
- Model:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Mvc7._1.Models
- {
- public class info
- {
- public string name { get; set; }
- public string pwd { get; set; }
- public int age { get; set; }
- public string sex { get; set; }
- public HttpPostedFileBase file { get; set; }
- }
- }
- View Code
- Views:
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <form action="/home/add" method="post" enctype="multipart/form-data">
- <table>
- <tr>
- <td > 用户名:</td>
- <td><input type="text" name="name" /></td>
- </tr>
- <tr>
- <td > 密码:</td>
- <td><input type="password" name="pwd" /></td>
- </tr>
- <tr>
- <td > 年龄:</td>
- <td><input type="text" name="age" /></td>
- </tr>
- <tr>
- <td > 性别:</td>
- <td>
- <input type="radio"name="sex"value="女" /> 女
- <input type="radio"name="sex" checked="checked" value="男" /> 男
- @* <input type="checkbox" name="sex"value="女" /> 女
- <input type="checkbox" name="sex" checked="checked" value="男" /> 男 *@
- </td>
- </tr>
- <tr><td > 请选择文件 </td>
- <td><input type="file" name="file" /></td>
- </tr>
- <tr><td colspan="2"><input type="submit" value="提交" /></td></tr>
- </table>
- </form>
- </div>
- <div>
- <p style="color:red">@ViewBag.msg</p>
- </div>
- </body>
- </html>
- View Code
来源: https://www.cnblogs.com/zeng-qh/p/9219864.html