MVC文件上传02-使用HttpPostedFileBase上传多个文件
2023-09-11 14:16:52 时间
本篇体验上传多个文件。兄弟篇为:
MVC文件上传01-使用jquery异步上传并客户端验证类型和大小
MVC最基本上传文件方式中的一个遗漏点
□ 前台视图部分
1: <% using(Html.BeginForm("FileUpload", "FileUpload", FormMethod.Post, new {enctype = "multipart/form-data"}) {)%>
2: <input name ="uploadFile" type="file" />
3: <input type="submit" value="Upload File" />
4: <%}%>
□ 控制器部分
1: [HttpMethod.Post]
2: public ActionResult FileUpload(HttpPostedFileBase uploadFile)
3: {
4: if(uploadFile.ContenctLength > 0)
5: {
6: //获得保存路径
7: string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
8: Path.GetFileName(uploadFile.FileName));
9: uploadFile.SaveAs(filePath);
10: }
11: return View();
12: }
以上的问题是:当没有文件上传的时候,会报错。需要判断HttpPostedFileBase实例是否为null
1: [HttpMethod.Post]
2: public ActionResult FileUpload(HttpPostedFileBase uploadFile)
3: {
4: if(uploadFile != null)
5: {
6: if(uploadFile.ContenctLength > 0)
7: {
8: //获得保存路径
9: string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
10: Path.GetFileName(uploadFile.FileName));
11: uploadFile.SaveAs(filePath);
12: }
13: }
14: return View();
15: }
上传多个文件
□ 控制器
● 由于接收多个文件,所以控制器方法的参数变成了IEnumerable<HttpPostedFileBase> files
● 变量files与前台视图的name属性值对应
● 如果没有指定的文件夹,就创建一个文件夹
1: using System;
2: using System.Collections.Generic;
3: using System.IO;
4: using System.Linq;
5: using System.Web;
6: using System.Web.Mvc;
7:
8: namespace MvcApplication2.Controllers
9: {
10: public class HomeController : Controller
11: {
12: public ActionResult Index()
13: {
14: return View();
15: }
16:
17: public ActionResult MultiUpload(IEnumerable<HttpPostedFileBase> files)
18: {
19: string pathForSaving = Server.MapPath("~/Uploads");
20: if (this.CreateFolderIfNeeded(pathForSaving))
21: {
22: foreach (var file in files)
23: {
24: if (file != null && file.ContentLength > 0)
25: {
26: var path = Path.Combine(pathForSaving, file.FileName);
27: file.SaveAs(path);
28: }
29: }
30: }
31:
32: return RedirectToAction("Index");
33: }
34:
35: // 检查是否要创建上传文件夹
36: private bool CreateFolderIfNeeded(string path)
37: {
38: bool result = true;
39: if (!Directory.Exists(path))
40: {
41: try
42: {
43: Directory.CreateDirectory(path);
44: }
45: catch (Exception)
46: {
47: //TODO:处理异常
48: result = false;
49: }
50: }
51: return result;
52: }
53: }
54: }
55:
□ Home/Index.cshtml视图
1: @{
2: ViewBag.Title = "Index";
3: Layout = "~/Views/Shared/_Layout.cshtml";
4: }
5:
6: @using (Html.BeginForm("MultiUpload", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
7: {
8: <input type="file" name="files" id="file1" /><br/>
9: <input type="file" name="files" id="file2" /><br/>
10: <input type="file" name="files" id="file3" /><br/>
11: <input type="submit" value="同时上传多个文件" />
12: }
注意:name属性值与控制器方法参数对应。
□ 结果
选择上传2个文件:
上传成功后,自动创建了文件夹Uploads:
Uploads中只有2个文件:
□ 为什么使用HttpPostedFileBase而不是FormCollection来接收文件
public sealed class FormCollection : NameValueCollection, IValueProvider
可见,FormCollection是键值集合,键是string类型,值是string类型,而上传的文件类型不是string,所以不能用FormCollection作为参数来接收文件。
参考资料
相关文章
- [Android Pro] 查看 keystore文件的签名信息 和 检查apk文件中的签名信息
- Spring MVC文件上传
- Spring MVC实现上传文件报错解决方案
- 安装完QQ必须要删除掉的几个恐怖文件
- java struts2入门学习--基于xml文件的声明式验证
- ajax利用html5新特性带进度条上传文件
- 文件上传漏洞WAF绕过方法
- Spring MVC 上传文件(upload files)
- Visual Studio 2013 无法使用:在文件中查找(Ctrl+Shift+F)
- ASP.NET MVC (三、表单与文件上传)
- WEB漏洞攻防 - 文件上传漏洞 -常规检测之客户端验证绕过
- 文件上传图片形式
- Qt error: C1075: 左侧的 大括号“{” 与文件结尾不匹配
- Http服务器实现文件上传与下载(五)
- Spring MVC 学习总结(五)——校验与文件上传
- leaflet使用L.geoJSON加载文件,参数style的使用方法(128)
- leaflet 上传KMZ文件,并在map上显示(062)
- 爬虫日记(54): Flask的上传文件
- 用ajaxFileUpLoad上传文件不能正确取得返回值的问题
- 1.4 DVWA亲测文件上传漏洞