• NetCore3.1 多文件上传


    十年河东,十年河西,莫欺少年穷

    学无止境,精益求精

    本篇博客主要用于个人记录,同时也分享给大家

    代码如下:

            [HttpPost]
            [Route("UploadFile")]
            public IActionResult UploadFile([FromForm] IFormFileCollection formFiles, string FileCate)
            {
                var result = _Service.UploadFile(formFiles, FileCate);
                return Ok(result);
            }

    主代码

     /// <summary>
            /// 上传图片
            /// </summary>
            /// <param name="formFile"></param>
            /// <param name="path">路劲</param>
            /// <returns></returns>
            public BaseResponse UploadFile(IFormFileCollection formFiles, string FileCate)
            {
    
                try
                {
                    List<UploadDocumentModel> fileLst = new List<UploadDocumentModel>();
                    if (formFiles.Count == 0)
                    {
                        return CommonBaseResponse.SetResponse<List<UploadDocumentModel>>(null, false, "请选择要上传的文件。");
                    }
                    var chekResult = CheckFile(formFiles);
                    if (!chekResult.IsSuccess)
                    {
                        return chekResult;
                    }
                    foreach (var file in formFiles)
                    {
                        var suffix = Path.GetExtension(file.FileName);//提取上传的文件文件后缀
                        var newFileName = Guid.NewGuid().ToString() + suffix;
                        var path = this._pictureOptions.BaseUrl + @"" + newFileName;
                        var XdPath = this._pictureOptions.BasePath + @"" + newFileName;
                        var fileSize = file.Length;
                        var fileModel = new UploadDocumentModel() { fileName = newFileName, fileSize = fileSize, suffix = suffix, path = path, fileCate = FileCate, XdPath = XdPath };
                        fileLst.Add(fileModel);
    
                        if (!System.IO.Directory.Exists(this._pictureOptions.BaseUrl))
                        {
                            System.IO.Directory.CreateDirectory(this._pictureOptions.BaseUrl);
                        }
                        using (var fs = System.IO.File.Create(path))
                        {
                            file.CopyTo(fs);
                            //Stream 都有 Flush() 方法,
                            //根据官方文档的说法
                            //“使用此方法将所有信息从基础缓冲区移动到其目标或清除缓冲区,或者同时执行这两种操作”
                            fs.Flush();
                        }
                    }
    
                    return CommonBaseResponse.SetResponse<List<UploadDocumentModel>>(fileLst, true);
                }
                catch (Exception ex)
                {
                    throw ExceptionHelper.GetAggregateException("SysDocumentService.UploadFile", "UploadFile", FileCate, ex);
                }
            }
            /// <summary>
            /// 检测文件
            /// </summary>
            /// <param name="formFiles"></param>
            /// <returns></returns>
            private BaseResponse CheckFile(IFormFileCollection formFiles)
            {
                foreach (var file in formFiles)
                {
                    var fileNameWithoutSuffix = Path.GetFileNameWithoutExtension(file.FileName);//文件名 不包含后缀
                    var suffix = Path.GetExtension(file.FileName);//提取上传的文件文件后缀
                    var fileSize = file.Length;
                    if (fileSize >= this._pictureOptions.MaxSize)
                    {
                        return CommonBaseResponse.SetResponse(false, "存在过大的文件。");
                    }
                    if (!this._pictureOptions.FileTypes.Contains(suffix))//检查文件格式
                    {
                        return CommonBaseResponse.SetResponse(false, "存在非法扩展名文件。");
                    }
                }
                return CommonBaseResponse.SetResponse(true);
            }

    相关参数,采用NEtCore的选项模式。Uploads 为IIS的虚拟目录。

      "FileOptions": {
        "FileTypes": ".gif,.jpg,.jpeg,.png,.bmp,.GIF,.JPG,.JPEG,.PNG,.BMP",
        "MaxSize": 10485760,
        "BaseUrl": "D:\dotnet\fileServer\WuanManagerFiles",
        "BasePath": "Uploads" 
      }

    注:FileCate可以不要。结合业务进行取舍。

    @天才卧龙的博客

  • 相关阅读:
    归并排序
    快速排序
    冒泡排序
    排序算法复杂度
    [LeetCode] 20. Valid Parentheses ☆(括号匹配问题)
    makefile编写helloworld
    shell的通俗理解
    PID三种参数的理解
    PID的原理
    PID控制温度
  • 原文地址:https://www.cnblogs.com/chenwolong/p/netcorefiles.html
Copyright © 2020-2023  润新知