Exemple #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("");
        }