检查多个文件的文件扩展名

时间:2016-01-05 04:16:39

标签: c# file-upload

我正在尝试将5个文件名合并为一个字符串,以检查通过用户上传的文件的扩展名,并且仅允许.jpg,.png和.jpeg,但我不确定我的方法是否正确,尤其是部分的:

string filess = "FileExtentio, FileExtentio2, FileExtentio3, FileExtentio4, FileExtentio5";

那么你能否告诉我正确的方法来做这部分代码

if (FileUploadImg1.HasFile || FileUploadImg2.HasFile || FileUploadImg3.HasFile || FileUploadImg4.HasFile || FileUploadImg5.HasFile)
                {
                    string FileExtentio = System.IO.Path.GetExtension(FileUploadImg1.FileName);
                    string FileExtentio2 = System.IO.Path.GetExtension(FileUploadImg2.FileName);
                    string FileExtentio3 = System.IO.Path.GetExtension(FileUploadImg3.FileName);
                    string FileExtentio4 = System.IO.Path.GetExtension(FileUploadImg4.FileName);
                    string FileExtentio5 = System.IO.Path.GetExtension(FileUploadImg5.FileName);

                    string filess = "FileExtentio, FileExtentio2, FileExtentio3, FileExtentio4, FileExtentio5";

                    if (filess.ToLower() == ".jpeg" && filess.ToLower() == ".png" && filess.ToLower() == ".jpeg")
                    {

3 个答案:

答案 0 :(得分:0)

string FileExtensio内容的代码块之上,添加以下内容:

string isValid = 'true';

然后,对于每个像

的行
string FileExtentio = System.IO.Path.GetExtension(FileUploadImg1.FileName);

更改为

string FileExtentioTemp = System.IO.Path.GetExtension(FileUploadImg1.FileName);
string FileExtentio = FileExtentioTemp == 'jpg' || FileExtenioTemp == 'png' || FileExtentioTemp == 'jpeg' ? FileExtentio : isValid = false;

然后,根据您的情况,只需说出

if(isValid == 'true'){
    doStuff;
}

答案 1 :(得分:0)

您可以使用非常有用的List<string>

List<string> fileExtentions = new List<string>();

if (FileUploadImg1.HasFile)
   fileExtentions.Add(System.IO.Path.GetExtension(FileUploadImg1.FileName));

if (FileUploadImg2.HasFile)
   fileExtentions.Add(System.IO.Path.GetExtension(FileUploadImg2.FileName));

if (FileUploadImg3.HasFile)
   fileExtentions.Add(System.IO.Path.GetExtension(FileUploadImg3.FileName));

if (FileUploadImg4.HasFile)
   fileExtentions.Add(System.IO.Path.GetExtension(FileUploadImg4.FileName));

if (FileUploadImg5.HasFile)
   fileExtentions.Add(System.IO.Path.GetExtension(FileUploadImg5.FileName));

if(fileExtentions.Any(f=>!f.Equals(".jpg", StringComparison.OrdinalIgnoreCase) &&
                         !f.Equals(".jpeg", StringComparison.OrdinalIgnoreCase) &&
                         !f.Equals(".png", StringComparison.OrdinalIgnoreCase))
{
   //Invalid file format detected
}

答案 2 :(得分:0)

我真的希望在asp中可以使用以下第一行。这个解决方案让你有机会以不同的方式为每个文件采取行动,并且在我看来看起来有点清洁。

var fileUploads = new List<FileUpload>
                      {
                          FileUploadImg1,
                          FileUploadImg2,
                          FileUploadImg3,
                          FileUploadImg4,
                          FileUploadImg5,
                      };

var allowedExtensions = new List<string>{ ".jpg", ".jpeg", ".png" };

foreach(FileUpload fileUpload in fileUploads)
{
    if(!fileUpload.HasFile)
    {
        continue;
    }  

    if(allowedExtensions.Contains(System.IO.Path.GetExtension(fileUpload.FileName)))
    {
        // do stuff with valid file
    }
    else
    {
        // do stuff with invalid file
    }
}