Beispiel #1
0
        private bool ValidateAndSavePostedFile(AuthUser operatorUser, HttpPostedFile postedFile, string errorName, string extraFileSuffix, out string fullPath)
        {
            fullPath = string.Empty;
            if (postedFile == null)
            {
                ThrowError(new CustomError(errorName, "请上传身份证" + (extraFileSuffix == "face"?"正面":"背面") + "扫描件"));
                return(false);
            }

            List <string> allowedFileType = new List <string>(new string[] { ".jpg", ".png", ".gif" });

            byte[] data = new byte[postedFile.ContentLength];

            if (data.Length > AllSettings.Current.NameCheckSettings.MaxIDCardFileSize)
            {
                ThrowError(new CustomError(errorName, "身份证扫描件文件大小不能超过" + ConvertUtil.FormatSize(AllSettings.Current.NameCheckSettings.MaxIDCardFileSize)));
                return(false);
            }

            postedFile.InputStream.Read(data, 0, data.Length);

            string fileType = Path.GetExtension(postedFile.FileName).ToLower();

            if (!allowedFileType.Contains(fileType) || !IOUtil.IsImageFile(data, ImageFileType.GIF | ImageFileType.JPG | ImageFileType.PNG))
            {
                ThrowError(new CustomError(errorName, "身份证扫描件格式不正确"));
                return(false);
            }

            string newFileName = string.Format("{0}-{1}{2}", operatorUser.UserID, extraFileSuffix, ".config");
            string path        = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_IDCard), operatorUser.UserID.ToString());

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path); //不做异常捕获
            }

            try
            {
                File.WriteAllBytes(IOUtil.JoinPath(path, newFileName), data);
            }
            catch (Exception ex)
            {
                ThrowError(new CustomError("发生了系统错误" + ex.Message));
                return(false);
            }

            fullPath = IOUtil.JoinPath(Globals.GetVirtualPath(SystemDirecotry.Upload_IDCard), operatorUser.UserID.ToString(), newFileName);

            return(true);
        }