asp.net core MVC 上传单文件与多文件

以上传 图片为例

1.model中 Student模型 增加一个string类型的 文件名称 属性,用于记录文件名 如:

 public class Student //Model名称
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public ClassNameEnum ClassName { get; set; }

        public string Email { get; set; }

        public string PhotoFileName { get; set; } //数据库中字段
    }

2.在 ViewModel 中把 PhotoFileName 属性 返回值 改为 IFormFile 类型,用于接受 文件 其他属性不变 如:

 public class StudentPhoto  //ViewModel名称
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public ClassNameEnum ClassName { get; set; }

        public string Email { get; set; }

        public IFormFile PhotoFileName { get; set; } //改变
    }

3.前端视图 添加前端代码,asp-for="@Model.PhotoFileName" 会自动生成 Tpye=file,如:

<input asp-for="@Model.PhotoFileName" class="form-control custom-file-input" />

4.构造函数 注入 IWebHostEnvironment 用于获取 asp.net core MVC WebRootPath(网站根目录)  本机的wwwroot路径


        private readonly IWebHostEnvironment hostEnvironment;

        public HomeController(IWebHostEnvironment hostEnvironment)
        {
            this.hostEnvironment = hostEnvironment;
        }

5.在控制器 方法中 添加 操作 如:

       [HttpPost]
        public IActionResult Create(StudentPhoto model)
        {
            if (ModelState.IsValid)
            {   
                //将 webRoot路径与 images 路径合并到一起
                string ImgWebPath = Path.Combine(hostEnvironment.WebRootPath, "images");

                //生成一个唯一的 文件名
                string fileName = Guid.NewGuid().ToString() + "_" + model.PhotoFileName.FileName;

                //组合绝对路径:images路径+文件名
                string FullName = Path.Combine(ImgWebPath, fileName);

                //将文件 拷贝到 images文件夹中
                model.PhotoFileName.CopyTo(new FileStream(FullName, FileMode.Create));
                Student student = new Student
                {
                    Name = model.Name,
                    Email = model.Email,
                    ClassName = model.ClassName,
                    PhotoFileName = fileName
                };
                //添加到数据库中
                Student newStudent = _studentRepository.AddStudent(student);
                return View("OneStudent", newStudent);
            }
            return View();
        }

多文件上传

1.改变  第2步 ViewModel 文件为:

 public class StudentPhoto  //ViewModel名称
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public ClassNameEnum ClassName { get; set; }

        public string Email { get; set; }

        public List<IFormFile> PhotoFileName { get; set; } //多文件上传
    }

2.修改 第3步 前端视图 增加 multiple  属性,支持多文件上传

<input asp-for="@Model.PhotoFileName" multiple class="form-control custom-file-input" />

最后 需要在 form表单添加 enctype 如: <form enctype="multipart/form-data">

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值