.net core webapi 上传图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41600552/article/details/83015725
 [HttpPost("api/QualificationOne/Upload")]
        public ActionResult Upload(IFormCollection Files, int EntId, int CrtUser)
        {
         
            try
            {
                //var form = Request.Form;//直接从表单里面获取文件名不需要参数
                string dd=  Files["File"];
                var  form = Files;//定义接收类型的参数
                Hashtable hash = new Hashtable();
                IFormFileCollection cols = Request.Form.Files;
                if (cols == null || cols.Count == 0)
                {
                    return Json(new { status = -1, message = "没有上传文件", data = hash });
                }
                 foreach (IFormFile file in cols)
                 {
                        //定义图片数组后缀格式
                        string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
                        //获取图片后缀是否存在数组中
                        string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
                        if (LimitPictureType.Contains(currentPictureExtension))
                        {

                        //为了查看图片就不在重新生成文件名称了
                        // var new_path = DateTime.Now.ToString("yyyyMMdd")+ file.FileName;
                         var new_path = Path.Combine("uploads/images/", file.FileName);
                            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", new_path);
                       
                            using (var stream = new FileStream(path, FileMode.Create))
                            {
                               
                                //图片路径保存到数据库里面去
                                bool flage=  QcnApplyDetm.FinancialCreditQcnApplyDetmAdd(EntId, CrtUser, new_path);
                                if (flage == true)
                                {
                                    //再把文件保存的文件夹中
                                    file.CopyTo(stream);
                                    hash.Add("file", "/" + new_path);
                                }
                            }
                        }
                        else
                        {
                            return Json(new { status = -2, message = "请上传指定格式的图片", data = hash });
                        }
                }
             
                return Json(new { status = 0, message = "上传成功", data = hash });
            }
            catch (Exception ex)
            {
               
                return Json(new { status = -3, message = "上传失败", data = ex.Message });
            }

        }

,这里有几个重点:

1:引用  IFormCollection 类 获取  Asp.Net.Core 中 Http 请求的一个类,就可以直接获取form表单中的参数,

   应为 ASP.NET Core 中,没有了 IIS ,它的 HttpContext 只能用 IFormCollection 生成获取form表单中的数据

命名空间为 :namespace Microsoft.AspNetCore.Http

2: 引用 IFormFileCollection 类 获取上传图片的文件名

      命名空间为 :namespace Microsoft.AspNetCore.Http

       IFormFile GetFile(string name);,获取一张图片的名称
        IReadOnlyList<IFormFile> GetFiles(string name);获取多张图片的名称

3:创建一个  Hashtable hash = new Hashtable(); 类 保存图片路劲进行返回数据

总结:

asp.net mvc 与 asp.net core 中上传图片区别点是

扫描二维码关注公众号,回复: 3865780 查看本文章

net core没有httpContext 不能采用这种方式获取 http请求中的form 数据,只能使用  IFormCollection 接口类

多熟悉操作IO流自能而能就可以轻松搞定上传图片

(总结的不到位请各位指教我在修改,如上代码经过考验可以直接使用)

猜你喜欢

转载自blog.csdn.net/weixin_41600552/article/details/83015725