netcore 图片上传

2021-01-10 13:33

阅读:655

标签:pos   exception   tco   img   header   task   env   form   file   

 public async Task AjaxUploud(IFormCollection formCollection)
        {
           // return Json("成功");

            if (HttpContext.Request.Form.Files.Count == 0)
            {
                throw new BizException("文件数为0");
            }

            var files = Request.Form.Files;
            //var fileUrl = await StoreUserPostedImageAsync(postFile, 0);
            Liststring> filePathResultList = new Liststring>();

            foreach (var file in files)
            {
                var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.ToString().Trim(");

                string filePath = hostingEnv.WebRootPath + $@"\uploud\Pictures\";

                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }

                string suffix = fileName.ToString().Split(.)[1];

                if (!pictureFormatArray.Contains(suffix))
                {
                   
                }

                fileName = Guid.NewGuid() + "." + suffix;

                string fileFullName = filePath + fileName;

                using (FileStream fs = System.IO.File.Create(fileFullName))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                filePathResultList.Add($"/uploud/Pictures/{fileName}");
            }
            var result = new
            {
                filePathResultList,
                filePathResultList.Count
            };

            return Json(result);


        }

技术图片

 

 前端传值 多了括号,导致后台接收不到图片

netcore 图片上传

标签:pos   exception   tco   img   header   task   env   form   file   

原文地址:https://www.cnblogs.com/njccqx/p/13500741.html


评论


亲,登录后才可以留言!