public void ProcessRequest(HttpContext context)
        {
            this.context = context;

            this.configFile = context.Request["configFile"];        // 配置文件
            string action = this.context.Request["Action"];         // 操作名称
            string type = context.Request["type"];                  // 类别
            string dirName = context.Request["dir"];                // 路径 = 类别
            HttpPostedFile file = context.Request.Files["imgFile"]; // 上传的文件
            string fileMaxSize = ConfigHelper.GetVal("FileMaxSize"); // 最大文件大小

            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();
            extTable.Add("image", "gif,jpg,jpeg,png,bmp");
            extTable.Add("flash", "swf,flv");
            extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
            extTable.Add("file", "rtf,txt,doc,docx,xls,xlsx,ppt,pptx,pdf,zip,rar,gz,bz2");

            if (file == null)
            {
                showError("请选择文件。");
            }

            string uploadUrl = GetUploadUrl();

            //文件保存目录URL
            string saveUrl = uploadUrl;
            //文件保存目录路径
            string savePath = context.Server.MapPath(uploadUrl);

            //最大文件大小
            int maxSize = 5; //5M
            if (!string.IsNullOrEmpty(fileMaxSize)) maxSize = Convert.ToInt32(fileMaxSize);
            maxSize = maxSize * 1024 * 1024;
            // 上传文件大小是否超过限制
            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                showError("上传文件大小超过限制。");
            }

            string fileName = file.FileName;
            // 获取扩展名
            string fileExt = Path.GetExtension(fileName).ToLower();
            // 上传文件是否为允许的文件类型
            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(((string)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((string)extTable[dirName]) + "格式。");
            }

            if (!Directory.Exists(savePath))
            {
                //showError("上传目录不存在。");
                Directory.CreateDirectory(savePath);
            }

            if (string.IsNullOrEmpty(dirName))
            {
                dirName = "image";
            }
            if (!extTable.ContainsKey(dirName))
            {
                showError("目录名不正确。");
            }

            //创建文件夹
            savePath += dirName + "\\";
            saveUrl += dirName + "/";
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
            savePath += ymd + "\\";
            saveUrl += ymd + "/";
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            // 新文件名
            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff_b", DateTimeFormatInfo.InvariantInfo) + fileExt;
            // 新文件的完成路径
            string filePath = savePath + newFileName;
            // 保存
            file.SaveAs(filePath);

            // 需要返回的文件
            string fileUrl = saveUrl + newFileName;

            // 如果是图片文件
            if (!string.IsNullOrEmpty(configFile) &&
                !string.IsNullOrEmpty(dirName) &&
                dirName.Equals("image") &&
                IsRealImage(file))
            {
                string thumbnailFile = newFileName.Replace("_b", "_s");
                string thumbnailFilePath = savePath + thumbnailFile;

                int thumbnailWidth = 0, thumbnailHeight = 0;
                int t = 0;
                if (!int.TryParse(type, out t))
                {
                    t = 0;
                }
                switch (t)
                {
                    case 1://1:图文
                    case 2://2:产品
                        thumbnailWidth = ConfigHelper.GetVal<int>(configFile, "NewsThumbnailWidth");
                        thumbnailHeight = ConfigHelper.GetVal<int>(configFile, "NewsThumbnailHeight");
                        break;
                    case 3://3:图片
                        thumbnailWidth = ConfigHelper.GetVal<int>(configFile, "ImageThumbnailWidth");
                        thumbnailHeight = ConfigHelper.GetVal<int>(configFile, "ImageThumbnailHeight");
                        break;
                    case 4://4:产品
                        thumbnailWidth = ConfigHelper.GetVal<int>(configFile, "ProductThumbnailWidth");
                        thumbnailHeight = ConfigHelper.GetVal<int>(configFile, "ProductThumbnailHeight");
                        break;
                }
                thumbnailWidth = (thumbnailWidth == 0) ? 200 : thumbnailWidth;
                thumbnailHeight = (thumbnailHeight == 0) ? 200 : thumbnailHeight;

                ImageHelper.CreateThumbnailMode mode = ImageHelper.CreateThumbnailMode.CTM_NONE;
                int m = ConfigHelper.GetVal<int>(configFile, "CreateThumbnailMode");
                mode = (ImageHelper.CreateThumbnailMode)m;

                ImageHelper imgHelper = new ImageHelper();
                imgHelper.WatermarkInfo = getWatermarkInfo();

                bool ret = true;
                if (mode != ImageHelper.CreateThumbnailMode.CTM_NONE && (thumbnailWidth > 0 && thumbnailHeight > 0))
                {
                    // 生成缩略图
                    ret = imgHelper.CreateThumbnail(filePath, thumbnailFilePath, thumbnailWidth, thumbnailHeight, mode);
                    if(ret) fileUrl = saveUrl + thumbnailFile;
                }
                // 添加水印
                if (imgHelper.WatermarkInfo.AddWatermark == 1 || imgHelper.WatermarkInfo.AddWatermark == 3)
                {
                    newFileName = "w_" + newFileName;
                    ret = imgHelper.addWaterMark(filePath, savePath + newFileName, true);
                    if (ret) fileUrl = saveUrl + newFileName;
                }
                if (imgHelper.WatermarkInfo.AddWatermark == 2 || imgHelper.WatermarkInfo.AddWatermark == 3)
                {
                    thumbnailFile = "w_" + thumbnailFile;
                    ret = imgHelper.addWaterMark(thumbnailFilePath, savePath + thumbnailFile, false);
                    if (ret) fileUrl = saveUrl + thumbnailFile;
                }
            }

            Hashtable hash = new Hashtable();
            hash["error"] = 0;
            hash["name"] = fileName;
            hash["ext"] = fileExt;
            hash["size"] = file.ContentLength;
            hash["url"] = fileUrl;
            context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
            if (action == "swfupload") // 如果是swfupload的请求,则直接返回文件路径
            {
                context.Response.Write(fileUrl);
            }
            else
            {
                context.Response.Write(JsonMapper.ToJson(hash));
            }
            context.Response.End();
        }
Exemple #2
0
        protected override void btnSave_Click(object sender, EventArgs e)
        {
            if (action.Equals("add"))
            {
                ad = new cmsAd();
            }
            else
            {
                ad = ds.GetModel(id);
                if (ad != null)
                {
                    ad.Attach();
                }
                else
                {
                    ad = new cmsAd();
                }
            }
            ad.Type = Convert.ToInt32(ddlType.SelectedValue);
            ad.Title = txtTitle.Text.Trim();

            if (hidPicType1.Value.Equals("1"))
            {
                string imgUrl = ""; int maxSize = 5; //5M
                string fileMaxSize = ConfigurationManager.AppSettings["FileMaxSize"];
                if (!string.IsNullOrEmpty(fileMaxSize))
                    maxSize = Convert.ToInt32(fileMaxSize);
                ImageHelper imgHelper = new ImageHelper();
                ImageHelper.UploadStatus status = imgHelper.UploadImage(fuPic1.PostedFile, maxSize, 0, 0, ImageHelper.CreateThumbnailMode.CTM_CUT, ref imgUrl);
                switch (status)
                {
                    case ImageHelper.UploadStatus.US_NOT_FOUND:
                        break;
                    case ImageHelper.UploadStatus.US_TOO_LONG:
                        ScriptUtil.Alert("上传文件大小超过限制。");
                        return;
                    case ImageHelper.UploadStatus.US_NOT_SUPPORT:
                        ScriptUtil.Alert("上传文件扩展名是不允许的扩展名。\n只允许gif,jpg,jpeg,png,bmp格式。");
                        return;
                    case ImageHelper.UploadStatus.US_FAILED:
                        ScriptUtil.Alert("上传文件时服务器发生错误。");
                        return;
                    case ImageHelper.UploadStatus.US_SUCCESSED:
                        break;
                }

                txtPic1.Text = imgUrl;
            }
            if (hidPicType2.Value.Equals("1"))
            {
                string imgUrl = "";
                int maxSize = 5; //5M
                string fileMaxSize = ConfigurationManager.AppSettings["FileMaxSize"];
                if (!string.IsNullOrEmpty(fileMaxSize))
                    maxSize = Convert.ToInt32(fileMaxSize);
                ImageHelper imgHelper = new ImageHelper();
                ImageHelper.UploadStatus status = imgHelper.UploadImage(fuPic2.PostedFile, maxSize, 0, 0, ImageHelper.CreateThumbnailMode.CTM_CUT, ref imgUrl);
                switch (status)
                {
                    case ImageHelper.UploadStatus.US_NOT_FOUND:
                        break;
                    case ImageHelper.UploadStatus.US_TOO_LONG:
                        ScriptUtil.Alert("上传文件大小超过限制。");
                        return;
                    case ImageHelper.UploadStatus.US_NOT_SUPPORT:
                        ScriptUtil.Alert("上传文件扩展名是不允许的扩展名。\n只允许gif,jpg,jpeg,png,bmp格式。");
                        return;
                    case ImageHelper.UploadStatus.US_FAILED:
                        ScriptUtil.Alert("上传文件时服务器发生错误。");
                        return;
                    case ImageHelper.UploadStatus.US_SUCCESSED:
                        break;
                }

                txtPic2.Text = imgUrl;
            }
            ad.Pics = txtPic1.Text + "|" + txtPic2.Text;
            ad.Urls = txtUrl1.Text + "|" + txtUrl2.Text;
            ad.Remark = txtMemo.Text;
            ad.IsEnabled = chkEnabled.Checked ? 1 : 0;
            try
            {
                int r = ds.SaveModel(ad);

                if (action.Equals("add"))
                {
                    ScriptUtil.ConfirmAndRedirect(@"广告添加成功!\n“确定”继续添加,“取消”则跳转到列表页。", "AdAdd.aspx?node=" + NodeCode, "AdList.aspx?node=" + NodeCode);
                }
                else
                {
                    ScriptUtil.ConfirmAndRedirect(@"广告修改成功!\n“确定”留在本页,“取消”则跳转到列表页。", "", "AdList.aspx?node=" + NodeCode);
                }
            }
            catch (Exception ex)
            {
                ScriptUtil.Alert(ex.Message);

                Logger.Error(ex.Message);
            }
        }