首页 > 解决方案 > 文件上传未检测到文件扩展名

问题描述

好的,我正在检查我的代码中的以下扩展。

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 文档找到它,当它清楚地存在时

标签: asp.net

解决方案


推荐阅读