Validate uploaded file extension

后端 未结 2 1043
逝去的感伤
逝去的感伤 2021-01-25 04:47

Uploading files works fine but now I\'m trying to validate file extensions and looks like there\'s some interference between FileUpload1 and FileUpload2

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 05:11

    bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            case ".png":
                return true;
            default:
                return false;
        }
    }
    
    if (CheckFileType(fuImage.FileName))
    {
     //..........
    }
    

    or use RegularExpressionValidator:

    
    

提交回复
热议问题