模型绑定&文件上传(解决浏览器兼容问题)

Controllers:

 1 using Mvc7._1.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.IO;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.Mvc;
 8 
 9 
10 namespace Mvc7._1.Controllers
11 {
12     public class HomeController : Controller
13     {
14         //
15         // GET: /Home/
16 
17         public ActionResult Index()
18         {
19             return View();
20         }
21 
22         public string test(string name, int? age)//使用模型绑定获取基本类型数据
23         {                                        //获取基本类型string int    int后的?表示可空类型  如不加上?输入类型不符将会报错
24 
25             return name + "\t" + age;
26         }
27 
28         [HttpPost] //只处理post提交的 不能处理get提交
29         public ActionResult add(info info)//使用默认模型绑定获取模型对象数据   页面name的字段和实体对象名称要一致
30         {
31             string msg = null;
32             if (info.file.ContentLength > 0)
33             {
34                 info.file.SaveAs(Server.MapPath("~/Content/upload/" + info.file.FileName));
35                 //IE 中报System.NotSupportedException:“不支持给定路径的格式。”
36                 // 这种方式在ie中不兼容所以要用Path.GetFileName();转换
37                 msg = "上传成功!";
38             }
39             else
40             {
41                 msg = "未选择文件";
42             }
43             ViewBag.msg = info.name + "\t" + info.pwd + "\t" + info.sex + "\t" + info.age + "\t" + msg;
44             return View("index");//调用index页面
45                                  //页面上需要输出 <div>
46                                  //<p style="color:red">@ViewBag.msg</p>
47                                  //</div>
48         }
49 
50         //可处理post 或 get请求    但是有文件上传时候用到Request.Files[0]的时候必须使用post方式提交
51         public string add()
52         {
53             info info = new info();
54             info.name = Request["name"];
55             info.age = int.Parse(Request["age"]);
56             info.sex = Request["sex"];
57             info.pwd = Request["pwd"];
58             info.file = Request.Files[0];//提交方式是post才可以get方式获取不到值
59             string msg = null;
60 
61             if (info.file.ContentLength > 0)
62             {
63 
64                 //info.file.SaveAs(Server.MapPath("~/Content/upload/") + info.file.FileName); 
65                 string FileName = Path.GetFileName(info.file.FileName);
66                 info.file.SaveAs(Server.MapPath("~/Content/upload/") + FileName);
67                 msg = "上传成功!";
68             }
69             else
70             {
71                 msg = "未选择文件";
72             }
73 
74             return info.name + "\t" + info.pwd + "\t" + info.sex + "\t" + info.age + "\t" + msg;
75         }
76     }
77 };

Model:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace Mvc7._1.Models
 7 {
 8     public class info
 9     {
10         public string  name { get; set; }
11         public string pwd { get; set; }
12         public int age { get; set; }
13         public string sex { get; set; }
14         public HttpPostedFileBase file { get; set; }
15     }
16 }
View Code

Views:

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>Index</title>
11 </head>
12 <body>
13     <div>
14        <form action="/home/add" method="post" enctype="multipart/form-data">
15            <table>
16                <tr>
17                    <td>用户名:</td>
18                    <td><input type="text" name="name" /></td>
19                </tr>
20                 <tr>
21                    <td>密码:</td>
22                    <td><input type="password" name="pwd" /></td>
23                </tr>
24                 <tr>
25                    <td>年龄:</td>
26                    <td><input type="text" name="age" /></td>
27                </tr>
28                 <tr>
29                    <td>性别:</td>
30                    <td>
31                        <input type="radio"name="sex"value=""  />32                        <input type="radio"name="sex" checked="checked" value="" />33                     @*   <input type="checkbox" name="sex"value=""  />34                        <input type="checkbox" name="sex" checked="checked" value="" />男*@
35                    </td>
36                </tr>
37                <tr><td>请选择文件</td>
38                    <td><input type="file" name="file" /></td>
39                </tr>
40               <tr><td colspan="2"><input type="submit" value="提交" /></td></tr>
41            </table>
42        </form>
43     </div>
44      <div>
45         <p style="color:red">@ViewBag.msg</p>
46     </div>
47 </body>
48 </html>
View Code

猜你喜欢

转载自www.cnblogs.com/zeng-qh/p/9219864.html