上传文件net core

https://blog.csdn.net/qq_32688731/article/details/100924211

 

控制器中

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="IsService"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> UserFeedbackProblemOfPicture(int userId, bool IsService)
        {
            try
            {
                var file = Request.Form.Files[0];
                var pictureurl = await _problemFeedbackService.UserFeedbackProblemOfPictureAsync(userId, file, IsService);
                if (pictureurl == null)
                {
                    return Json(new Str.Result<string>(Str.StatusCode.MissingParam));
                }
                return Json(new Str.Result<string>(Str.StatusCode.Success, pictureurl));
            }
            catch (Exception ex)
            {
                return Json(new Str.Result<string>(Str.StatusCode.MissingParam));
            }
        }

service中的方法

        /// <summary>
        /// 用户反馈问题(图片)
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="file"></param>
        /// <param name="IsService"></param>
        /// <returns></returns>
        public async Task<string> UserFeedbackProblemOfPictureAsync(int userId, IFormFile file, bool IsService)
        {
            var datePath = DateTime.Now.ToString("yyyyMMdd");
            var fileName = DESEncrypt.CryptMd5String(Guid.NewGuid().ToString()) + System.IO.Path.GetExtension(file.FileName);
            string savePath = String.Format(FileConfigValue.UploadFolder, FileConfigValue.FuncPath, datePath);
            string filePath = string.Format(FileConfigValue.UploadFolder, FileConfigValue.FuncPath, datePath) + fileName;
            string url = string.Format(FileConfigValue.WebPerfix, FileConfigValue.FuncPath, datePath) + fileName;
            var cndImgUrl = url;
            if (file != null)
            {
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }
          //其他逻辑处理
               
            }
            return null;
        }

 

posted @ 2020-07-08 20:22  青兰柳  阅读(180)  评论(0编辑  收藏  举报