asp.net - 文件上传未检测到文件扩展名
问题描述
好的,我正在检查我的代码中的以下扩展。
FileAttachments.FileAttachmentTypes fileAttachmentType = FileAttachments.FileAttachmentTypes.None;
conts 下面的字符串在常量文件中被地狱化 public const string wordExtensions = "docx;docm;doc"; 公共常量字符串 imageExtensions = "gif;tiff;tif;png;eps;raw;bmp;jpeg;jpg"; 公共常量字符串 excelExtensions = "csv;xml;xls;xlsb;xlsm;xlsx";
然后在这里解析
我从 File Info 类中获取我的扩展名
foreach (var fileAttachments in FormFile) {
if (fileAttachments.Length > 0) {
string filePath = Path.Combine(hostingEnvironment.WebRootPath, "Uploads");
var tennantId = GetCurrentTennantId().Result;
var settings = _context.SystemSetup.FirstOrDefault().UploadFolderPath;
string uniqueFilename = Guid.NewGuid().ToString() + "_" + fileAttachments.FileName;
string savedFileName = Path.Combine(filePath, uniqueFilename);
FileInfo infoFile = new FileInfo(savedFileName);
string extension = infoFile.Extension.Replace(".","");
在这里,我在分号分隔的字符串中检查了上面的文件扩展名参数
if (extension.Contains(Constants.imageExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Image;
} else if (extension.Contains("pdf")) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.PDF;
} else if (extension.Contains(Constants.videoFormats)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Video;
} else if (extension.Contains(Constants.excelExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Excel;
} else if (extension.Contains(Constants.wordExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Word;
} else if (extension.Contains(Constants.audioExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Voice;
}
}
类型只是将它们分类为前端显示选项卡的区域
public enum FileAttachmentTypes {
[Display(Name = "Microsoft Word")]
Word = 1,
[Display(Name = "Microsoft Excel")]
Excel = 2,
[Display(Name = "Image")]
Image = 3,
[Display(Name = "Voice Recordings")]
Voice = 4,
[Display(Name = "PDF")]
PDF = 5,
[Display(Name = "Video")]
Video = 6,
[Display(Name = "Evidence")]
Evidence = 7,
[Display(Name = "None")]
None = 8,
[Display(Name = "Notes")]
NOTES = 9,
[Display(Name = "Shared Case")]
SharedCase = 10,
[Display(Name = "Created Case")]
CreatedCase = 11
}
我的问题是为什么它没有为扩展 docx 的 word 文档找到它,当它清楚地存在时
解决方案
推荐阅读
- java - 我在使用 Kotlin Android Studio 4 时遇到 Gradle 同步问题
- flutter - 如何防止在 setState 后关闭键盘?
- python - 将 Django 连接到 PostgreSQL - 尽管安装,但加载 psycopg2 模块时出错
- matplotlib - 在 python 3.9 中安装 matplotlib 时出错
- oracle - Internet Explorer 中的特殊字符(点)在 Weblogic 和 Oracle 中未正确保存
- javascript - 如何从用户脚本访问 webpack 模块?
- linux - 磁盘上的可用空间不可用
- azure-functions - 值不能为空。(参数'uriString')
- python - 为每组数据打印一个列表到新行
- snowflake-cloud-data-platform - 将 SQL Server 转换为 Snowflake SQL,如何编写 INSERT 语句以将查询中的数据放入临时表?