Example #1
0
        public static void ImageWater(UploadConfig uploadobj, string imgFilePath)
        {
            ImageWatermark iwobj = new ImageWatermark(0, 0, imgFilePath);

            if (uploadobj.WatermarkType == "0") //水印为文字类型
            {
                iwobj.Quality  = 100;
                iwobj.Title    = uploadobj.WatermarkText;
                iwobj.FontSize = int.Parse(uploadobj.WatermarkFontSize);
                switch (uploadobj.WatermarkFontStyle)
                {
                case "1":
                    iwobj.StrStyle = FontStyle.Bold;
                    break;

                case "2":
                    iwobj.StrStyle = FontStyle.Italic;
                    break;

                case "4":
                    iwobj.StrStyle = FontStyle.Underline;
                    break;

                case "8":
                    iwobj.StrStyle = FontStyle.Strikeout;
                    break;
                }
                iwobj.FontColor       = ColorTranslator.FromHtml("#" + uploadobj.WatermarkFontColor);
                iwobj.BackGroudColor  = Color.White;
                iwobj.FontFamilyName  = uploadobj.WatermarkFontType;
                iwobj.Waterpos        = uploadobj.WatermarkLocation;
                iwobj.FontBorderColor = ColorTranslator.FromHtml("#" + uploadobj.WatermarkFontBorderColor);
                iwobj.FontBorderSize  = int.Parse(uploadobj.WatermarkFontBorder);

                iwobj.Watermark();
            }
            else
            {
                iwobj.Waterpos  = uploadobj.WatermarkLocation;
                iwobj.WaterPath = System.Web.HttpContext.Current.Server.MapPath("~/" + uploadobj.WatermarkPic);
                iwobj.WaterPicturemark();
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string msg = "";

            if (uploadobj.IsEnableUpload == "1")                    // 判断是否允许上传
            {
                string noExt = SystemConst.NOT_ALLOWED_UPLOAD_TYPE; // 不允许上传类型
                int    num   = Utils.ParseInt(Request.Form["hidNum"], 0);

                for (int i = 1; i <= num; i++)
                {
                    HttpPostedFile fil = Request.Files["file" + i];
                    if (!string.IsNullOrEmpty(fil.FileName))
                    {
                        if (fil.ContentLength > MaxSize)
                        {
                            msg += "文件" + i + "上传失败,原因是文件大小超过最大上传限制!<br>";
                            continue;
                        }
                        string fileName = Path.GetFileName(fil.FileName);
                        string fileExt  = Path.GetExtension(fileName).ToLower(); //上传文件扩展名

                        if (AllowExt.IndexOf(fileExt.Replace(".", "")) == -1)
                        {
                            msg += "文件" + i + "上传失败,原因是文件类型不正确!<br>";
                            continue;
                        }

                        if (noExt.IndexOf(fileExt) != -1)
                        {
                            msg += "文件" + i + "上传失败,原因是文件类型“" + fileExt + "”的文件不允许上传!<br>";
                            continue;
                        }

                        if (SavePath.IndexOf(":") == -1)  //判断输入的是虚拟路径
                        {
                            SavePath = Server.MapPath(GetVirtualPath + SavePath);
                        }

                        SavePath = SavePath + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/";
                        if (!Directory.Exists(SavePath))
                        {
                            Directory.CreateDirectory(SavePath);
                        }

                        string saveName = DateTime.Now.ToString("yyyyMMddHHmmss");  // 上传文件名

                        fileName = SavePath + saveName + fileExt;
                        URL      = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + saveName + fileExt;

                        try
                        {
                            fil.SaveAs(fileName);                                             //保存至服务器
                            DelFile();
                            if (this.UpType == "image" && uploadobj.IsEnableWatermark == "1") //如果上传图片,则判断是否需要打水印
                            {
                                ImageWatermark iwobj = new ImageWatermark(0, 0, fileName);
                                if (uploadobj.WatermarkType == "0") //水印为文字类型
                                {
                                    iwobj.Quality  = 100;
                                    iwobj.Title    = uploadobj.WatermarkText;
                                    iwobj.FontSize = int.Parse(uploadobj.WatermarkFontSize);
                                    switch (uploadobj.WatermarkFontStyle)
                                    {
                                    case "1":
                                        iwobj.StrStyle = FontStyle.Bold;
                                        break;

                                    case "2":
                                        iwobj.StrStyle = FontStyle.Italic;
                                        break;

                                    case "4":
                                        iwobj.StrStyle = FontStyle.Underline;
                                        break;

                                    case "8":
                                        iwobj.StrStyle = FontStyle.Strikeout;
                                        break;
                                    }
                                    iwobj.FontColor       = ColorTranslator.FromHtml("#" + uploadobj.WatermarkFontColor);
                                    iwobj.BackGroudColor  = Color.White;
                                    iwobj.FontFamilyName  = uploadobj.WatermarkFontType;
                                    iwobj.Waterpos        = uploadobj.WatermarkLocation;
                                    iwobj.FontBorderColor = ColorTranslator.FromHtml("#" + uploadobj.WatermarkFontBorderColor);
                                    iwobj.FontBorderSize  = int.Parse(uploadobj.WatermarkFontBorder);

                                    iwobj.Watermark();
                                }
                                else
                                {
                                    iwobj.Waterpos = uploadobj.WatermarkLocation;
                                    //iwobj.Height = 100;
                                    //iwobj.Width = 100;
                                    iwobj.WaterPath = Server.MapPath("~/" + uploadobj.WatermarkPic);
                                    iwobj.WaterPicturemark();
                                }
                            }
                            if (Request.Form["chkIshumbnail"] == "1")                                      //判断是否上传的是否需生成缩略图
                            {
                                MakeThumbnail(fileName, fileName + ".gif", thumbWidth, thumbHeight, "HW"); // 生成缩略图方法
                            }
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                        }
                    }



                    //if (controlType == "0")         // 将上传文件名加到下拉列表控件中
                    //{
                    //    jsExe = "<script type=\"text/javascript\">parent.document.getElementById(\"" + elementName + "\").options[parent.document.getElementById(\"" + elementName + "\").options.length] = new Option(\"" + URL + "\", \"" + URL + "\");parent.Closed();</script>";
                    //}
                    //else if (controlType == "2")    // 多文件自定义系统字段
                    //{
                    //    jsExe = "<script type=\"text/javascript\">parent.AddItemToMultiFile(\"" + URL + "\", \"" + elementName + "\", \"" + getSizeControl + "\", true);parent.Closed();</script>";
                    //}
                    //else                            // 将上传文件地址附加至文本框
                    //{
                    //    jsExe = "<script language='javascript'>parent.document.getElementById(\"" + elementName + "\").value=\"" + URL + "\";if(parent.document.getElementById(\"" + elementName + "_Del\") != null){parent.document.getElementById(\"" + elementName + "_Del\").disabled=false;};";

                    //    if (!string.IsNullOrEmpty(getSizeControl) && getSizeControl != "undefined")
                    //    {
                    //        jsExe += "parent.document.getElementById(\"" + getSizeControl + "\").value=\"" + txtFileUpload.FileContent.Length + "\";";
                    //    }
                    //    jsExe += "if(parent.document.getElementById(\"" + elementName + "_Look\") != null){parent.document.getElementById(\"" + elementName + "_Look\").disabled=false;}; parent.Closed();</script>";
                    //}
                }
            }
        }
Example #3
0
        void UploadFiles()
        {
            if (uploadobj.IsEnableUpload == "1")  //判断是否允许上传
            {
                HttpPostedFile file = Request.Files["upload"];

                if (file.ContentLength > 0)
                {
                    #region 变量
                    string formName    = Request.QueryString["FormName"];      //表单名称
                    string elementName = Request.QueryString["ElementName"];   //控件名称
                    string saveType    = Request.QueryString["SaveType"];      //保存文件名类型,1=用上传文件名保存,否则用系统时间重命名保存

                    string controlType = Request.QueryString["ControlType"];   //控件类型,0=select,其他为input;

                    string noExt = SystemConst.NOT_ALLOWED_UPLOAD_TYPE;        //不允许上传类型

                    string fileName = string.Empty;                            //文件名

                    string saveName = DateTime.Now.ToString("yyyyMMddHHmmss"); //上传文件名

                    string reFilePath = URL;                                   //返回文件路径,如果保存路径填写的是绝对地址,则返回文件路径为域名+系统创建路径,如果为相对地址,则为:域名+相对路径+系统创建路径
                    string jsExe      = string.Empty;                          //输出字符串

                    string fileExt = string.Empty;                             //上传文件扩展名


                    fileName = Path.GetFileName(file.FileName).Trim();
                    fileExt  = Path.GetExtension(fileName).ToLower(); //上传文件扩展名

                    #endregion

                    if (file.ContentLength > MaxSize)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">alert('" + Utils.GetResourcesValue("Common", "UploadErr1") + Math.Round(float.Parse(MaxSize.ToString()) / 1048576, 2) + "M!');</script>");
                        return;
                    }

                    if (SavePath.IndexOf(":") == -1)  //判断输入的是虚拟路径
                    {
                        SavePath = Server.MapPath(GetVirtualPath + SavePath);
                    }

                    if (AllowExt.IndexOf(fileExt.Replace(".", "")) == -1)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">alert('" + Utils.GetResourcesValue("Common", "UploadExtErr") + "!');</script>");
                        return;
                    }

                    if (noExt.IndexOf(fileExt) != -1)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">alert('" + Utils.GetResourcesValue("Common", "UploadExtErr1") + "!');</script>");
                        return;
                    }

                    SavePath = SavePath + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/";
                    if (!Directory.Exists(SavePath))
                    {
                        Directory.CreateDirectory(SavePath);
                    }

                    if (saveType == "1")  //使用原文件名进行保存到服务器
                    {
                        int fileNameLength = fileName.LastIndexOf(".");
                        saveName = fileName.Substring(0, fileNameLength);
                    }

                    fileName = SavePath + saveName + fileExt;
                    URL      = URL + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + saveName + fileExt;
                    if (File.Exists(fileName) && saveType == "1")
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">alert('" + Utils.GetResourcesValue("Common", "UploadFileExistErr") + "!');</script>");
                        return;
                    }

                    try
                    {
                        file.SaveAs(fileName);                                            //保存至服务器
                        DelFile();
                        if (this.UpType == "image" && uploadobj.IsEnableWatermark == "1") //如果上传图片,则判断是否需要打水印
                        {
                            ImageWatermark iwobj = new ImageWatermark(0, 0, fileName);
                            if (uploadobj.WatermarkType == "0") //水印为文字类型

                            {
                                iwobj.Quality  = 100;
                                iwobj.Title    = uploadobj.WatermarkText;
                                iwobj.FontSize = int.Parse(uploadobj.WatermarkFontSize);
                                switch (uploadobj.WatermarkFontStyle)
                                {
                                case "1":
                                    iwobj.StrStyle = FontStyle.Bold;
                                    break;

                                case "2":
                                    iwobj.StrStyle = FontStyle.Italic;
                                    break;

                                case "4":
                                    iwobj.StrStyle = FontStyle.Underline;
                                    break;

                                case "8":
                                    iwobj.StrStyle = FontStyle.Strikeout;
                                    break;
                                }
                                iwobj.FontColor       = ColorTranslator.FromHtml("#" + uploadobj.WatermarkFontColor);
                                iwobj.BackGroudColor  = Color.White;
                                iwobj.FontFamilyName  = uploadobj.WatermarkFontType;
                                iwobj.Waterpos        = uploadobj.WatermarkLocation;
                                iwobj.FontBorderColor = ColorTranslator.FromHtml("#" + uploadobj.WatermarkFontBorderColor);
                                iwobj.FontBorderSize  = int.Parse(uploadobj.WatermarkFontBorder);

                                iwobj.Watermark();
                            }
                            else
                            {
                                iwobj.Waterpos = uploadobj.WatermarkLocation;
                                //iwobj.Height = 100;
                                //iwobj.Width = 100;
                                iwobj.WaterPath = Server.MapPath("~/" + uploadobj.WatermarkPic);
                                iwobj.WaterPicturemark();
                            }
                        }
                        if (Request.Form["chkIshumbnail"] == "1")  //判断是否上传的是否需生成缩略图

                        {
                            MakeThumbnail(fileName, fileName + ".gif", int.Parse(uploadobj.ThumbnailWidth), int.Parse(uploadobj.ThumbnailHeight), "HW");     // 生成缩略图方法
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }


                    if (controlType == "0")  //将上传文件名加到下拉列表控件中

                    {
                        jsExe = "<script language='javascript'>var newOpt=window.opener.document.createElement(\"option\");newOpt.text=\"" + saveName + fileExt + "\";newOpt.value=\"" + URL + "\";newOpt.selected=true;  window.opener.document.all." + elementName + ".options.add(newOpt); self.close();</script>";
                    }
                    else if (controlType == "-1")  //用showModalDialog打开上传文件,ckeditor编辑器上传用此方法

                    {
                        jsExe = "<script language='javascript'>window.returnValue='" + URL + "';window.close();</script>";
                    }
                    else if (controlType == "-2")
                    {
                        jsExe = "<script language='javascript'>window.parent.CKEDITOR.tools.callFunction(" + Request.QueryString["CKEditorFuncNum"] + ",'" + URL + "','') ;</script>";
                    }
                    else
                    {
                        jsExe = "<script language='javascript'>tmp=eval(\"opener." + formName + "." + elementName + "\");tmp.value='" + URL + "';tmp=eval(\"opener." + formName + "." + elementName + "_Del\");if(tmp){tmp.disabled=false;};";

                        if (Request.QueryString["GetSizeControl"] != null && Request.QueryString["GetSizeControl"] != "")
                        {
                            jsExe += "tmp=eval(\"opener." + formName + "." + Request.QueryString["GetSizeControl"] + "\");tmp.value='" + file.ContentLength + "';";
                        }
                        jsExe += "tmp=eval(\"opener." + formName + "." + elementName + "_Look\");if(tmp){tmp.disabled=false;}; self.close();</script>";
                    }
                    Response.Write(jsExe);
                }
            }
        }