Asp.net core 迁移 文件上传和保存

12 篇文章 0 订阅
6 篇文章 0 订阅

原先的做法,使用HttpFileCollectionBase来接收前台发过来的文件流然后通过以下方式保存

HttpPostedFileBase fileSave = Request.Files["imageFiles"];
string filepath = filePath + "/" + fileName;
fileSave.SaveAs(filepath);

在aspnet core中则使用IFormFile来接收,操作如下:

通过Ajax上传的文件如何获取到,且保存到服务器相应的目录底下_environment.WebRootPath指的是项目中wwwroot静态文件目录:

要取得系统变量ASPNETCORE_ENVIRONMENT,在3.0版本之前可以通过注入IHostingEnvironment来获取,3.x通过IWebHostEnvironment 请看如下代码片段:

public class Startup
    {
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment()) { }
            app.Run(async (context) => {
                await context.Response.WriteAsync($"EnvironmentName: {env.EnvironmentName},IsDevelopment: {env.IsDevelopment()}");
            });
        }
    }

然后在每个控制器中通过构造函数传递,环境对象

IWebHostEnvironment _environment;
        public DepartmentDoctorController(IWebHostEnvironment environment)//在startup中已经做了注册可以直接通过控制器的构造函数进行传递
        {
            _environment = environment;
        }

最后看一下,文件获取和保存的代码

/// <summary>
        /// 文件上传接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadFile(UploadPostData uploadData)
        {
            IFormFileCollection flist = HttpContext.Request.Form.Files;
            List<IFormFile> formfile = new List<IFormFile>();
            foreach (IFormFile item in flist)
            {
                formfile.Add(item);
            }
            return Json(UploadFileApi.GetUploadResult(HttpContext, uploadData, formfile));
        }
//保存的方式相同
/// <summary>
        /// 上传头像图片.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult UploadDoctorImg(string id)
        {
            string filePath = _environment.WebRootPath + @"/css/WeChatWebSite/DoctorFaceImage/"; 
            string imagePath = string.Empty;
            bool result = false;
            IFormFile imageFiles = Request.Form.Files["faceImage"];
            if (imageFiles != null && !string.IsNullOrWhiteSpace(imageFiles.FileName))
            {
                imagePath = _DepartmentDoctor.SaveDocFaceImage(imageFiles, filePath);
                result = _DepartmentDoctor.UpdateDoctorImg(id, imagePath).Result;
            }
            return Json(result);
        }


/// <summary>
        /// 保存医生头像.
        /// </summary>
        /// <param name="fileImage"></param>
        /// <returns></returns>
        public string SaveDocFaceImage(IFormFile fileImage, string filePath)
        {
//HttpPostedFileBase fileSave = file;
//string filepath = filePath + "/" + fileName;
            //fileSave.SaveAs(filepath);

            string macPath = ServerUrlHelper.GetMapPath(filePath);//HttpContext.Current.Server.MapPath(SaveDocFaceImagePath);
            if (!System.IO.Directory.Exists(macPath))
            {
                Directory.CreateDirectory(macPath); // 不存在就创建目录 
            }
            using (FileStream filestream = System.IO.File.Create(macPath + fileImage.FileName))
            {
                fileImage.CopyTo(filestream);
                filestream.Flush();
            }
            //fileImage.SaveAs(macPath + fileImage.FileName);
            return SaveDocFaceImagePath + fileImage.FileName;
        }

以上就是我在迁移aspnet webapi时替换的文件上传方式,有更好的方式,可交流

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新时代丘鸣山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值