Example #1
0
        /// <summary>
        /// 保存单个文件到磁盘
        /// </summary>
        /// <param name="fileContent">文件内容</param>
        /// <param name="fileNewName">文件新名称</param>
        /// <returns>是否保存成功</returns>
        internal static Boolean InternalAdminSaveUploadFile(Byte[] fileContent, String fileNewName)
        {
            if (!AdminManager.HasPermission(PermissionType.Administrator))
            {
                throw new NoPermissionException();
            }

            if (String.IsNullOrEmpty(fileNewName))
            {
                throw new InvalidInputException("Filename can not be NULL!");
            }

            FileInfo fi = new FileInfo(fileNewName);

            if (!UploadsManager.CheckFileExtension(fi.Extension, ALLOW_EXTENSTIONS))
            {
                throw new InvalidInputException("Filename is INVALID!");
            }

            String savePath = Path.Combine(ConfigurationManager.UploadDirectoryPath, fileNewName);

            if (File.Exists(savePath))
            {
                throw new InvalidInputException("Filename exists!");
            }

            File.WriteAllBytes(savePath, fileContent);

            return(true);
        }
Example #2
0
        /// <summary>
        /// 保存单个文件到磁盘
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <returns>是否保存成功</returns>
        public static IMethodResult AdminSaveUploadFile(HttpPostedFileBase file)
        {
            if (!AdminManager.HasPermission(PermissionType.Administrator))
            {
                throw new NoPermissionException();
            }

            if (file == null)
            {
                return(MethodResult.FailedAndLog("No file was uploaded!"));
            }

            if (String.IsNullOrEmpty(file.FileName))
            {
                return(MethodResult.FailedAndLog("Filename can not be NULL!"));
            }

            FileInfo fi = new FileInfo(file.FileName);

            if (!UploadsManager.CheckFileExtension(fi.Extension, ALLOW_EXTENSTIONS))
            {
                return(MethodResult.FailedAndLog("Filename is INVALID!"));
            }

            if (file.ContentLength <= 0)
            {
                return(MethodResult.FailedAndLog("You can not upload empty file!"));
            }

            String fileNewName = MD5Encrypt.EncryptToHexString(fi.Name + DateTime.Now.ToString("yyyyMMddHHmmssffff"), true) + fi.Extension;
            String savePath    = Path.Combine(ConfigurationManager.UploadDirectoryPath, fileNewName);

            if (File.Exists(savePath))
            {
                return(MethodResult.FailedAndLog("Filename exists!"));
            }

            file.SaveAs(savePath);

            return(MethodResult.SuccessAndLog <String>(fileNewName, "Admin upload file, name = {0}, origin = {1}", fileNewName, fi.Name));
        }