Ejemplo n.º 1
0
    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[0];//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;

        string fileExt          = Path.GetExtension(uploadFileName).ToLower();
        string saveDoc          = DateTime.Now.ToString(@"yy\\MM\\dd\\");
        string rndFileName      = FancyFix.Tools.Usual.Common.GetDataShortRandom();
        string dirPath          = UploadConfig.PathFormat;// uploadFileName;//PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
        string newFileName      = saveDoc + rndFileName + fileExt;
        string newSmallFileName = saveDoc + rndFileName + "s" + fileExt;
        var    savePath         = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);

        try
        {
            if (!Directory.Exists(dirPath + saveDoc))
            {
                Directory.CreateDirectory(dirPath + saveDoc);
            }
            File.WriteAllBytes(dirPath + newFileName, uploadFileBytes);
            ImageTools.SetImgSize(dirPath + newFileName, 800, 800);
            if (UploadConfig.PathFormat.IndexOf("product") > 0)
            {
                Setting setting = UploadProvice.Instance().Settings["product"];
                if (setting.AddWaterMark)
                {
                    ImageTools.AddWaterMark(dirPath + newFileName, dirPath + newFileName, Server.MapPath(setting.WaterMarkImgOrTxt), WaterMarkType.ImageMark, WaterMarkPosition.Right_Bottom, setting.Transparency);
                }
            }
            //Tools.Tool.ImageTools.CreateSmallImage(dirPath + newFileName, dirPath + newSmallFileName, 200, 200);
            Result.Url   = FancyFix.Tools.Usual.Utils.GetLastFolderName(dirPath) + "/" + newFileName;
            Result.State = UploadState.Success;
        }
        catch (Exception e)
        {
            Result.State        = UploadState.FileAccessError;
            Result.ErrorMessage = e.Message;
            FancyFix.Tools.Tool.LogHelper.WriteLog(typeof(UploadHandler), e);
        }
        finally
        {
            WriteResult();
        }
    }