예제 #1
0
        /// <returns>上传成功返回"",并填充 Model.UploadFile</returns>
        /// <param name="vid">上传配置模块id,即UploadConfig_Id</param>
        /// <param name="key">随机key</param>
        /// <param name="userId">上传者id</param>
        /// <param name="userName">上传者UserName</param>
        /// <param name="remotePicUrl">远程图片的url地址</param>
        /// <param name="m_r">Model.UploadFile</param>
        /// <returns>上传成功返回"",并填充 Model.UploadFile</returns>
        public string Upload_RemotePic(int vid, string key, int userId, string userName, string remotePicUrl, UploadFile m_r)
        {
            #region 检查参数
            //---------------------------------------------------
            if (vid < 1 || key.Length < 10)
            {
                return "缺少参数:key或sid";
            }
            //---------------------------------------------------

            #region 检查登陆
            m_r.UserId = userId;
            m_r.UserName = userName;

            if (m_r.UserId == 0)
            {
                return "您的权限不足!";
            }
            #endregion

            //---------------------------------------------------
            UploadConfig mC = Read_UploadConfig(vid);
            if (mC.Id != vid)
            {
                return "缺少参数:UploadConfig_Id!";
            }

            if (mC.IsPost != 1)
            {
                return "系统暂时禁止上传文件2!";
            }

            if (mC.IsEditor != 1)
            {
                return "非编辑器类别!";
            }

            mC.UploadType_TypeKey = "image";
            #endregion

            //----------------------------------------------
            #region 生成暂时目录
            string sCfgSavePath = new Uploader().SavePath;
            string sSavePath = DirFileHelper.FixDirPath(sCfgSavePath + mC.SaveDir) + DateTime.Now.ToString("yyMM") + "/";
            if (!DirFileHelper.CheckSaveDir(sSavePath))
            {
                return "SavePath设置不当:" + sSavePath + ", 或权限不足!";
            }

            string sServerDir = sCfgSavePath + "remote/";
            if (!DirFileHelper.CheckSaveDir(sServerDir))
            {
                return "ServerDir设置不当:" + sServerDir + ", 或权限不足!";
            }
            //----------------------------------------------
            string sSrcName = StringHelper.Left(DirFileHelper.GetFileName(remotePicUrl), 90);
            string sFileExt = DirFileHelper.GetFileExtension(sSrcName);

            //因部部分网站不是标准的jpg、gif扩展名,所以修改下面代码
            if (sFileExt.Length > 0)
            {
                string sAllowed = ",jpg,gif,png,bmp,";
                string sExt = "," + sFileExt.ToLower() + ",";
                if (sAllowed.IndexOf(sExt) == -1)
                {
                    sFileExt = "jpg";
                }
            }
            else
            {
                sFileExt = "jpg";
            }
            //----------------------------------------------

            string sNewFile = DirFileHelper.GetRndFileName("." + sFileExt);

            if (sServerDir.IndexOf(":") < 0)
            {
                sServerDir = DirFileHelper.FixDirPath(DirFileHelper.GetMapPath(sServerDir));
            }
            string sNewRoot = System.IO.Path.Combine(sServerDir, sNewFile);
            #endregion

            //----------------------------------------------
            #region 上传到暂时目录
            try
            {
                var wc = new System.Net.WebClient();
                wc.DownloadFile(remotePicUrl, sNewRoot);
            }
            catch (Exception ex)
            {
                //throw ex;
                return ex.Message.ToLower();
            }

            if (!DirFileHelper.IsExistFile(sNewRoot))
            {
                return "上传失败";
            }
            #endregion

            //----------------------------------------------
            #region 判断是否真实图片格式,并取得图片宽高
            int ww = 0, hh = 0;
            if (!Uploader.Get_Pic_WW_HH(sNewRoot, out ww, out hh))
            {
                DirFileHelper.DeleteFile(sNewRoot);
                return "非法格式!不是图片文件。";
            }

            int iMaxSize = mC.PicSize;
            long iFileSize = DirFileHelper.GetFileSize(sNewRoot);
            /*
            if (iFileSize > iMaxSize)
            {
                return "上传文件大小超过了限制.最多上传(" + DirFileHelper.FmtFileSize2(iMaxSize) + ").";
            }
            */
            #endregion

            #region 把上传的暂时文件复制到相关模块目录中
            string sNewPath = sSavePath + sNewFile;
            string orgImg = DirFileHelper.GetFilePathPostfix(sNewPath, "o");

            //复制到原始图
            DirFileHelper.CopyFile(sNewRoot, orgImg);

            //删除暂时上传的图片
            DirFileHelper.DeleteFile(sNewRoot);

            //生成相关缩略图
            OneMakeThumbImage(sNewPath, mC);

            #endregion

            //----------------------------------------------
            #region 保存入数据库
            m_r.UploadConfig_Id = mC.Id;
            m_r.JoinName = mC.JoinName;
            m_r.JoinId = 0;

            m_r.UserType = mC.UserType;
            m_r.UserIp = IpHelper.GetUserIp();
            m_r.AddDate = DateTime.Now;
            m_r.InfoText = "";
            m_r.RndKey = key;

            m_r.Name = sNewFile;
            m_r.Path = sNewPath;
            m_r.Src = sSrcName;
            m_r.Ext = sFileExt;

            m_r.Size = ConvertHelper.Cint0(iFileSize);
            m_r.PicWidth = ww;
            m_r.PicHeight = hh;

            //保存入数据库
            Add_UploadFile(m_r);
            #endregion

            //------------------------------------
            //上传成功,输出结果
            return "";
        }
예제 #2
0
        /// <returns>上传成功返回"",并填充 UploadFile</returns>
        /// <param name="vid">上传配置模块id,即Id</param>
        /// <param name="key">随机key</param>
        /// <param name="userId">上传者id</param>
        /// <param name="userName">上传者UserName</param>
        /// <param name="m_r">UploadFile</param>
        /// <param name="filePostName">上传文件框控件的名称,默认"imgFile",uploadify 默认 "Filedata"</param>
        /// <param name="userType">0=未知,1=后台管理员上传,2=前台会员上传</param>
        /// <param name="isEditor">从GetAction返回判断是否为编辑器</param>
        /// <param name="isSwf">是否通过flash上传</param>
        /// <returns>上传成功返回"",并填充 UploadFile</returns>
        public string Upload_Web(int vid, string key, int userId, string userName,
            UploadFile m_r, string filePostName = "imgFile",
            int userType = 1, bool isEditor = false, bool isSwf = false)
        {
            #region 检查参数
            //---------------------------------------------------
            if (vid < 1 || key.Length < 10)
            {
                return "缺少参数:key或sid";
            }

            string dir = RequestHelper.GetKeyChar("dir");//编辑器专用:image,flash,media,file
            if (dir.Length > 0)
            {
                if (Array.IndexOf("image,flash,media,file".Split(','), dir) == -1)
                {
                    return "缺少参数:dir";
                }
            }
            //---------------------------------------------------
            UploadConfig mC = Read_UploadConfig(vid);
            if (mC == null || mC.Id != vid)
            {
                return "缺少参数:上传配置Id设置不正确!";
            }

            if (mC.IsPost != 1)
            {
                return "系统暂时禁止上传文件2!";
            }

            if (mC.IsEditor == 1 && isEditor == false)
            {
                return "非编辑器类别!";
            }

            if (mC.IsSwf == 0 && isSwf == true)
            {
                return "不能从flash中上传!";
            }
            #endregion

            //---------------------------------------------------
            #region 检查登陆
            m_r.UserId = 0;
            if (mC.UserType == 1)//管理员
            {
                if (userType == 1)
                {
                    m_r.UserId = userId;
                    m_r.UserName = userName;
                }
            }
            else
            {
                if (userType == 2)//一般会员
                {
                    m_r.UserId = userId;
                    m_r.UserName = userName;
                }
            }

            if (m_r.UserId == 0)
            {
                return "您的权限不足!";
            }
            #endregion

            //------------------------------------------------
            #region 设置上传参数
            var oUp = new Uploader();

            oUp.IsEnabled = true;
            if (isSwf)
            {
                oUp.IsChkSrcPost = false;  //如果swf提交,必须设置为 o_up.isChkSrcPost = false;
            }
            else
            {
                //o_up.isChkSrcPost = (m_c.isChkSrcPost == "1");  //如果swf提交,必须设置为 o_up.isChkSrcPost = false;
                oUp.IsChkSrcPost = true;
            }

            oUp.CutType = ConvertHelper.Cint0(mC.CutType);

            oUp.FilePostName = filePostName;

            if (isEditor && mC.UploadType_TypeKey == "editor")
            {
                mC.UploadType_TypeKey = dir;
            }
            oUp.AllowedExt = Get_Ext(mC.UploadType_TypeKey);
            oUp.MaxSize = (mC.UploadType_TypeKey == "image") ? ConvertHelper.Cint0(mC.PicSize) : ConvertHelper.Cint0(mC.FileSize);
            oUp.SavePath = mC.SaveDir;

            oUp.SetPic((mC.IsFixPic == 1), ConvertHelper.Cint0(mC.PicWidth), ConvertHelper.Cint0(mC.PicHeight), ConvertHelper.Cint0(mC.PicQuality));
            oUp.SetBig((mC.IsBigPic == 1), ConvertHelper.Cint0(mC.BigWidth), ConvertHelper.Cint0(mC.BigHeight), ConvertHelper.Cint0(mC.BigQuality));
            oUp.SetMid((mC.IsMidPic == 1), ConvertHelper.Cint0(mC.MidWidth), ConvertHelper.Cint0(mC.MidHeight), ConvertHelper.Cint0(mC.MidQuality));
            oUp.SetMin((mC.IsMinPic == 1), ConvertHelper.Cint0(mC.MinWidth), ConvertHelper.Cint0(mC.MinHeight), ConvertHelper.Cint0(mC.MinQuality));
            oUp.SetHot((mC.IsHotPic == 1), ConvertHelper.Cint0(mC.HotWidth), ConvertHelper.Cint0(mC.HotHeight), ConvertHelper.Cint0(mC.HotQuality));

            oUp.IsWaterPic = (mC.IsWaterPic == 1);
            #endregion

            #region 上传
            //------------------------------------------------
            bool isOk = oUp.UploadFile();
            if (!isOk)
            {
                //上传出错
                return StringHelper.XssTextClear(oUp.GetErrMsg() + mC.Id);
            }
            #endregion

            //----------------------------------------------------------------
            #region 保存入数据库
            m_r.UploadConfig_Id = mC.Id;
            m_r.JoinName = mC.JoinName;
            m_r.JoinId = 0;

            m_r.UserType = mC.UserType;
            m_r.UserIp = IpHelper.GetUserIp();
            m_r.AddDate = DateTime.Now;
            m_r.InfoText = "";
            m_r.RndKey = key;

            m_r.Name = oUp.NewFile;
            m_r.Path = oUp.NewPath;
            m_r.Src = StringHelper.Left(oUp.SrcName, 90);
            m_r.Ext = oUp.FileExt;

            m_r.Size = oUp.GetFileSize();
            m_r.PicWidth = oUp.NewWidth;
            m_r.PicHeight = oUp.NewHeight;

            //保存入数据库
            Add_UploadFile(m_r);
            #endregion

            //------------------------------------
            //上传成功,输出结果
            return "";
        }
예제 #3
0
        /// <returns>上传成功返回"",并填充 UploadFile(AspNet上传控件专用)</returns>
        /// <param name="oFile">System.Web.HttpPostedFile</param>
        /// <param name="vid">上传配置模块id,即Id</param>
        /// <param name="key">随机key</param>
        /// <param name="userId">上传者id</param>
        /// <param name="userName">上传者UserName</param>
        /// <param name="m_r">UploadFile</param>
        /// <param name="userType">0=未知,1=后台管理员上传,2=前台会员上传</param>
        /// <returns>上传成功返回"",并填充 UploadFile</returns>
        public string Upload_AspNet(System.Web.HttpPostedFile oFile, int vid, string key, int userId, string userName,
            UploadFile m_r, int userType = 1)
        {
            #region 检查参数
            //---------------------------------------------------
            if (vid < 1 || key.Length < 10)
            {
                return "缺少参数:key或sid";
            }

            //---------------------------------------------------
            UploadConfig mC = Read_UploadConfig(vid);
            if (mC.Id != vid)
            {
                return "缺少参数:Id!";
            }

            if (mC.IsPost != 1)
            {
                return "系统暂时禁止上传文件2!";
            }

            if (mC.IsEditor == 1)
            {
                return "非编辑器类别!";
            }
            #endregion

            //---------------------------------------------------
            #region 检查登陆
            m_r.UserId = 0;
            if (mC.UserType == 1)//管理员
            {
                if (userType == 1)
                {
                    m_r.UserId = userId;
                    m_r.UserName = userName;
                }
            }
            else
            {
                if (userType == 2)//一般会员
                {
                    m_r.UserId = userId;
                    m_r.UserName = userName;
                }
            }

            if (m_r.UserId == 0)
            {
                return "您的权限不足!";
            }
            #endregion

            //------------------------------------------------
            #region 设置上传参数
            var oUp = new Uploader();

            oUp.IsEnabled = true;
            oUp.IsChkSrcPost = true;
            oUp.CutType = ConvertHelper.Cint0(mC.CutType);
            oUp.AllowedExt = Get_Ext(mC.UploadType_TypeKey);
            oUp.MaxSize = (mC.UploadType_TypeKey == "image") ? ConvertHelper.Cint0(mC.PicSize) : ConvertHelper.Cint0(mC.FileSize);
            oUp.SavePath = mC.SaveDir;

            oUp.SetPic((mC.IsFixPic == 1), ConvertHelper.Cint0(mC.PicWidth), ConvertHelper.Cint0(mC.PicHeight), ConvertHelper.Cint0(mC.PicQuality));
            oUp.SetBig((mC.IsBigPic == 1), ConvertHelper.Cint0(mC.BigWidth), ConvertHelper.Cint0(mC.BigHeight), ConvertHelper.Cint0(mC.BigQuality));
            oUp.SetMid((mC.IsMidPic == 1), ConvertHelper.Cint0(mC.MidWidth), ConvertHelper.Cint0(mC.MidHeight), ConvertHelper.Cint0(mC.MidQuality));
            oUp.SetMin((mC.IsMinPic == 1), ConvertHelper.Cint0(mC.MinWidth), ConvertHelper.Cint0(mC.MinHeight), ConvertHelper.Cint0(mC.MinQuality));
            oUp.SetHot((mC.IsHotPic == 1), ConvertHelper.Cint0(mC.HotWidth), ConvertHelper.Cint0(mC.HotHeight), ConvertHelper.Cint0(mC.HotQuality));

            oUp.IsWaterPic = (mC.IsWaterPic == 1);
            #endregion

            #region 上传
            //------------------------------------------------
            bool isOk = oUp.UploadFile(oFile);
            if (!isOk)
            {
                //上传出错
                return StringHelper.XssTextClear(oUp.GetErrMsg() + mC.Id);
            }
            #endregion

            //----------------------------------------------------------------
            #region 保存入数据库
            m_r.UploadConfig_Id = mC.Id;
            m_r.JoinName = mC.JoinName;
            m_r.JoinId = 0;

            m_r.UserType = mC.UserType;
            m_r.UserIp = IpHelper.GetUserIp();
            m_r.AddDate = DateTime.Now;
            m_r.InfoText = "";
            m_r.RndKey = key;

            m_r.Name = oUp.NewFile;
            m_r.Path = oUp.NewPath;
            m_r.Src = StringHelper.Left(oUp.SrcName, 90, false);
            m_r.Ext = oUp.FileExt;

            m_r.Size = oUp.GetFileSize();
            m_r.PicWidth = oUp.NewWidth;
            m_r.PicHeight = oUp.NewHeight;

            //保存入数据库
            Add_UploadFile(m_r);
            #endregion

            //------------------------------------
            //上传成功,输出结果
            return "";
        }