Exemple #1
0
        public override string Process()
        {
            byte[] uploadFileBytes = null;
            string uploadFileName  = null;

            if (UploadConfig.Base64)
            {
                uploadFileName  = UploadConfig.Base64Filename;
                uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
            }
            else
            {
                var file = Request.Files[Request["key"] ?? UploadConfig.UploadFieldName];
                uploadFileName = file.FileName;

                if (!CheckFileType(uploadFileName))
                {
                    Result.State = UploadState.TypeNotAllow;
                    return(WriteResult());
                }
                if (!CheckFileSize(file.ContentLength))
                {
                    Result.State = UploadState.SizeLimitExceed;
                    return(WriteResult());
                }

                uploadFileBytes = new byte[file.ContentLength];
                try
                {
                    file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
                }
                catch (Exception)
                {
                    Result.State = UploadState.NetworkError;
                    return(WriteResult());
                }
            }

            Result.OriginFileName = uploadFileName;


            var savePath  = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat, this.OrgCode);
            var localPath = Server.MapPath(savePath);

            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(localPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(localPath));
                }
                File.WriteAllBytes(localPath, uploadFileBytes);
                //加水印
                //if (BaseController.IsAddWaterImage)
                //{
                //    WaterImageManage wim = new WaterImageManage();
                //    var index = savePath.LastIndexOf(@"/", StringComparison.Ordinal);
                //    var head = savePath.Substring(0, index + 1);
                //    var foot = savePath.Substring(index + 1);
                //    var newFileName = wim.DrawImage(foot, Server.MapPath("~/Content/WaterImage.jpg"),
                //                                    BaseController.WaterImageAlpha, ImagePosition.RigthBottom,
                //                                    Path.GetDirectoryName(localPath) + @"\");
                //    File.Delete(localPath);
                //    Result.Url = head + newFileName;
                //}
                //else
                {
                    Result.Url = savePath;
                }
                Result.State = UploadState.Success;
            }
            catch (Exception e)
            {
                Result.State        = UploadState.FileAccessError;
                Result.ErrorMessage = e.Message;
            }
            finally
            {
            }

            return(WriteResult());
        }
        public override void Process()
        {
            byte[] uploadFileBytes = null;
            string uploadFileName  = null;

            if (UploadConfig.Base64)
            {
                uploadFileName  = UploadConfig.Base64Filename;
                uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
            }
            else
            {
                var file = Request.Files[UploadConfig.UploadFieldName];
                uploadFileName = file.FileName;

                if (!CheckFileType(uploadFileName))
                {
                    Result.State = UploadState.TypeNotAllow;
                    WriteResult();
                    return;
                }
                if (!CheckFileSize(file.ContentLength))
                {
                    Result.State = UploadState.SizeLimitExceed;
                    WriteResult();
                    return;
                }

                uploadFileBytes = new byte[file.ContentLength];
                try
                {
                    file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
                }
                catch (Exception)
                {
                    Result.State = UploadState.NetworkError;
                    WriteResult();
                }
            }

            Result.OriginFileName = uploadFileName;

            var savePath  = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
            var localPath = Server.MapPath(savePath);

            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(localPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(localPath));
                }
                File.WriteAllBytes(localPath, uploadFileBytes);
                Result.Url   = savePath;
                Result.State = UploadState.Success;
            }
            catch (Exception e)
            {
                Result.State        = UploadState.FileAccessError;
                Result.ErrorMessage = e.Message;
            }
            finally
            {
                WriteResult();
            }
        }