应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

.net6,.net core 文件上传封装。枚举使用。封装通用的文件上传

197人阅读 2023/2/24 21:59 总访问:5182488 评论:0 收藏:0 手机
分类: .NET Core

封装的代码如下

  1. using Microsoft.AspNetCore.Http;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace WY.JBLand.API.Tools
  8. {
  9. public enum UpLoadFileState { Sucess, TypeNoSupport, NoFile };
  10. public class UpLoadFileResult
  11. {
  12. public string FilePath { get; set; }
  13. public UpLoadFileState UpLoadFileState { get; set; }
  14. }
  15. /// <summary>
  16. /// 文件上传工具类封装
  17. /// </summary>
  18. public class UpLoadFileTools
  19. {
  20. public UpLoadFileResult FileUpLoad(IFormFileCollection files, string[] limitTypes, string savePath)
  21. {
  22. UpLoadFileResult upLoadFileResult = new UpLoadFileResult();
  23. if (files == null || files.Count == 0)
  24. {
  25. upLoadFileResult.UpLoadFileState = UpLoadFileState.NoFile;
  26. return upLoadFileResult;
  27. }
  28. foreach (IFormFile file in files)
  29. {
  30. //定义图片数组后缀格式
  31. //string[] LimitPictureType = { ".XLS" };
  32. //获取图片后缀是否存在数组中
  33. string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
  34. if (!limitTypes.Contains(currentPictureExtension))
  35. {
  36. upLoadFileResult.UpLoadFileState = UpLoadFileState.TypeNoSupport;
  37. return upLoadFileResult;
  38. }
  39. //文件重命名防止重复
  40. var newName = Guid.NewGuid().ToString("N") + currentPictureExtension.ToLower();
  41. var new_path = Path.Combine("uploads/files/", newName);
  42. //判断一下存储路径是否存在,不存在就创建
  43. //var savaDir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads/files");
  44. var savaDir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", savePath);
  45. if (!Directory.Exists(savaDir))
  46. Directory.CreateDirectory(savaDir);
  47. string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", new_path);
  48. using (var stream = new FileStream(path, FileMode.Create))
  49. {
  50. file.CopyTo(stream);
  51. stream.Flush();
  52. upLoadFileResult.FilePath = path;
  53. }
  54. }
  55. upLoadFileResult.UpLoadFileState = UpLoadFileState.Sucess;
  56. return upLoadFileResult;
  57. }
  58. }
  59. }

调用

  1. ReturnModel<int> returnModel = new ReturnModel<int>();
  2. UpLoadFileTools upLoadFileTools = new UpLoadFileTools();
  3. IFormFileCollection files = Request.Form.Files;
  4. //定义图片数组后缀格式
  5. string[] LimitTypes = { ".XLS" };
  6. string savePath = "uploads/files";
  7. UpLoadFileResult upLoadFileResult = upLoadFileTools.FileUpLoad(files, LimitTypes, savePath);
  8. if (upLoadFileResult.UpLoadFileState == UpLoadFileState.NoFile)
  9. {
  10. returnModel.Code = Enum.ResultCode.MESSAGE_ERROR;
  11. returnModel.Msg = "没有上传文件,无法导入,请检查";
  12. return returnModel;
  13. }
  14. if (upLoadFileResult.UpLoadFileState == UpLoadFileState.TypeNoSupport)
  15. {
  16. returnModel.Code = Enum.ResultCode.MESSAGE_ERROR;
  17. returnModel.Msg = "暂时只支持excel导入,请检查";
  18. return returnModel;
  19. }

欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

Quartz.net实例动态改变周期调度misfire、Cron

Quartz:Java编写的开源的任务调度作业框架 类似Timer之类定时执行的功能,但是更强大Quartz.NET:是把Quartz转成C# NuGet...

.net Windows服务发布、安装、卸载、监听脚本服务调试

一、脚本 为方便不用每次都去写安装卸载的脚本1.安装脚本@echooff @echo开始安装【服务】 %SystemRoot%\Microsoft.NET\Fr...

nginx常用命令nginx启动命令nginx重启命令nginx关闭命令nginx测试配置文件是否正确nginx nginx.pid文件丢失报错

启动命令:start nginx 关闭命令:nginx -s stop nginx -s quit nginx -s stop与nginx -s quit区别 Quit is a graceful shu...

DevExpress.XtraSpreadsheet.SpreadsheetControl控件 加载excel模板

stringpath=&quot;文件路径&quot;; DevExpress.XtraSpreadsheet.SpreadsheetControlspreadsheetControl=newDevExpress.Xtr...

上传文件到服务器及 下载到 客户端

usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Net; usingSystem.IO; namespaceCo...

使用OutLook发送邮件

publicstaticvoidOutlook(stringSubject,stringTextBody,stringFromAdd,stringFromPass,stringTo,stringCC,List&lt;string&...

类型“DbSet”在未引用的程序集中定义必须添加对程序集“EntityFramework Version=5.0.0.0 Culture=neutral PublicKeyToken=b7

在用mvc+ef的时候在DAL层引用上下文信息的时候会报出下面错误其实就是没得EntityFromwork,打开vs项目,点击工具,选择NuGe...

SQL Server 中使用游标

--声明一个游标 DECLAREMyCursorCURSOR FORSELECTTOP5FBookName,FBookCodingFROMTBookInfo//定义一个叫MyCursor的游标,...

C委托与事件

1.什么是委托?  委托在C#里的意义和在现实里差不多,从字面意思理解即可。举个例子:领导委托小张去传递个文件,这就是...

正则表达式匹配中文标点符号

//匹配这些中文标点符号。?!,、;:“”‘&#39;()《》〈〉【】『』「」﹃﹄〔〕…—~﹏¥ varreg=/[\u3002|\uff1f|\...

泛型简单介绍

说到了泛型,就介绍下泛型泛型不是特指具体类型,是一种可变类型,可以把他看做一个类型占位符,根据传入的类型 延迟声明具...

数据读取器与指定的"xx"不兼容某个类型为"xx"的成员在同名的数据读取器中没有对应的列

报错的地方var result= _db.Database.SqlQuery&lt;SMachine&gt;(sql).FirstOrDefault();经过分析,是因为SqlQuery方法查询...

远程服务器返回错误: (403) 已禁止

今天调用接口的时候报的错。我们只要加上这两句代码就行了呢HttpWebRequestreq=(HttpWebRequest)HttpWebRequest.Create(url...

NPOI读取excelexcel 导入日期类型读取

NPOI是一个优秀的操作excel的库,可以很方便的进行excel的读取与导出NPOI读取excelpublicActionResultReadExcel() { //打...

NPOI导出excel根据模板导出Excel

使用NPOI导出excel///&lt;summary&gt; ///导出excel(下载excel) ///&lt;/summary&gt; publicvoidToExcel() { HSSFWo...