Example #1
0
        /// <summary>
        /// 输出结果
        /// </summary>
        /// <param name="url">上传之后文件的完整路径</param>
        /// <param name="SaveMethod">保存方法</param>
        /// <param name="errMsg">错误信息</param>
        protected virtual void ResultOutput(string url, FU_SaveMethod SaveMethod, string errMsg)
        {
            url = url.Replace("//", "/");
            url = config.PerUrl + url;
            string json = string.Format("{{iSuccess:{0},msg:\"{1}\",url:\"{2}\",method:\"{3}\"}}",
                                        (SaveMethod == FU_SaveMethod.fail ? 0 : 1), jsEncode(errMsg), jsEncode(url), SaveMethod);
            string ret = config.Return;

            if (config.returnMode == FUC_RETURN.redirect)
            {
                ret = ret.Replace("{fileUrl}", Server.UrlEncode(url));
                ret = ret.Replace("{json}", Server.UrlEncode(json));
                ret = ret.Replace("{msg}", Server.UrlEncode(errMsg));
                ret = ret.Replace("{method}", Server.UrlEncode(SaveMethod.ToString()));
                Response.Redirect(ret);
            }
            else
            {
                ret = ret.Replace("{fileUrl}", url);
                ret = ret.Replace("{json}", json);
                ret = ret.Replace("{msg}", errMsg);
                ret = ret.Replace("{method}", SaveMethod.ToString());
                Response.Write(ret);
            }
        }
Example #2
0
        public void ProcessRequest(HttpContext context)
        {
            if (Request.Files.Count == 0)
            {
                ResultOutput(string.Empty, FU_SaveMethod.fail, "上传数据丢失!");
                return;
            }
            //验证身份等信息
            string ret = PerCheck();

            if (!string.IsNullOrEmpty(ret))
            {
                ResultOutput(string.Empty, FU_SaveMethod.fail, ret);
                return;
            }

            uFile = Request.Files[0];
            if (!config.VerifyFileType(uFile.FileName))
            {
                ResultOutput(string.Empty, FU_SaveMethod.fail, "不允许上传此文件类型!");
                return;
            }
            string uFileName = uFile.FileName;
            string fileExt   = Regex.Match(uFile.FileName, @"\.[^\.]+$").Value;

            uFileName = uFileName.Substring(0, uFileName.Length - fileExt.Length);
            int i = uFileName.Replace('/', '\\').LastIndexOf('\\');

            if (i != -1)
            {
                uFileName = uFileName.Substring(i + 1);
            }

            if (!string.IsNullOrEmpty(fileExt))
            {
                fileExt = fileExt.Substring(1).ToLower();
            }


            if (uFile.ContentLength > config.MaxSize)
            {
                ResultOutput(string.Empty, FU_SaveMethod.fail, string.Format("不允许上传大于{0}Kb的文件。上传的文件超出了{1}Byte",
                                                                             config.MaxSize / 1024, uFile.ContentLength - config.MaxSize));
                return;
            }
            Stream filestream = uFile.InputStream;

            Stream tStm = null;

            try
            {
                tStm = LogoPrint(filestream, ref fileExt);
            }
            catch (Exception ex)
            {
                ResultOutput(string.Empty, FU_SaveMethod.fail, "添加水印出错!" + ex.Message);
                return;
            }
            if (tStm != filestream)
            {
                filestream.Close();
                filestream.Dispose();
                filestream = tStm;
            }

            tStm = null;

            string fileName = getStorePath(uFileName, fileExt);

            try
            {
                FU_SaveMethod SaveMethod = SaveToFile(filestream, fileName);
                OnSaved(fileName);
                ResultOutput(fileName, SaveMethod, "ok");
            }
            catch (Exception ex)
            {
                ResultOutput(string.Empty, FU_SaveMethod.fail, ex.Message);
            }
        }