12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace XdCxRhDW.Dto.Attribute
- {
- /// <summary>
- /// 文件存在性验证,会验证本地文件是否存在
- /// </summary>
- public class FileMustExistAttribute : ValidationAttribute
- {
- /// <summary>
- ///
- /// </summary>
- public FileMustExistAttribute()
- {
- }
- /// <summary>
- /// 文件验证
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public override bool IsValid(object value)
- {
- if (value.GetType() != typeof(string))
- {
- throw new Exception($"{nameof(FileMustExistAttribute)}只能用于string类型!");
- }
- string fileName = value.ToString();
- if(string.IsNullOrWhiteSpace(fileName) )
- {
- ErrorMessage= "字段 {0} 值不能为空";
- return false;
- }
- if (fileName.Contains("\\") || fileName.Contains("/"))
- {
- ErrorMessage = $"文件[{value}]格式错误,必须是上传后返回的名称";
- return false;
- }
- ErrorMessage = $"文件[{value}]不存在,请先上传";
- string localFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", fileName);
- return File.Exists(localFile);
- }
- }
- /// <summary>
- /// 双行根验证,会验证格式
- /// </summary>
- public class TleStrAttribute : ValidationAttribute
- {
- /// <summary>
- ///
- /// </summary>
- public TleStrAttribute()
- {
- }
- /// <summary>
- /// 文件验证
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public override bool IsValid(object value)
- {
- if (value.GetType() != typeof(string))
- {
- throw new Exception($"{nameof(TleStrAttribute)}只能用于string类型!");
- }
- string tleStr = value.ToString();
- if (string.IsNullOrWhiteSpace(tleStr))
- {
- ErrorMessage = "字段 {0} 值不能为空";
- return false;
- }
- if (!tleStr.Contains(";"))
- {
- ErrorMessage = $"双行根数[{value}]格式错误,(line1和line2用分号拼到一起)";
- return false;
- }
- return true;
- }
- }
- }
|