Пример #1
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <summary>
        /// 上传文件
        /// </summary>
        public static UploadModel UploadFile(HttpPostedFileBase file, UNCConfig cfg)
        {
            UploadModel   model        = new UploadModel();
            List <string> enabledTypes = new List <string>()
            {
                "image/pjpeg", "image/x-png", "image/bmp", "image/png", "image/jpeg"
            };

            //model.FileName = string.Format("{0}.jpg{1}", file.ContentLength, Path.GetExtension(file.FileName));
            model.FileName = string.Format("{0}.jpg", file.ContentLength, Path.GetExtension(file.FileName));

            model.FileSize   = ChatHelper.FormatFileSize(file.ContentLength);
            model.ReturnFlag = false;



            if (System.Web.HttpContext.Current.Request.Headers["ocx"] == null && !enabledTypes.Exists(type => type == file.ContentType))
            {
                model.Message = "上传失败:不支持的文件格式。(仅支持.jpg|.png|.bmp格式)";
            }
            else
            {
                string fileServer = System.Configuration.ConfigurationManager.AppSettings["fileServer"];
                try
                {
                    using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
                    {
                        if (unc.NetUseWithCredentials(cfg.UNCPath,
                                                      cfg.User,
                                                      cfg.Domain,
                                                      cfg.Password))
                        {
                            var now      = DateTime.Now;
                            var yyyyMM   = now.ToString("yyyyMM");
                            var yyyyMMdd = now.ToString("yyyyMMdd");
                            var dir      = cfg.UNCPath + "\\" + yyyyMM + "\\" + yyyyMMdd + "\\";
                            var filename = Utils.SHA1Stream(file.InputStream) + ".jpg";

                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            file.SaveAs(dir + filename);
                            model.FileName   = Path.GetFileName(file.FileName);
                            model.FileUrl    = fileServer + yyyyMM + "/" + yyyyMMdd + "/" + filename;
                            model.ReturnFlag = true;
                            model.Message    = "上传成功。";
                        }
                    }
                }
                catch (Exception ex)
                {
                    model.Message = ex.Message;
                }
            }
            return(model);
        }
Пример #2
0
        private static void TryWriteLog(Exception ex, string stackTrace)
        {
            try
            {
                string fileName = string.Format(@"{0}logs\app_{1:yyyyMM}\{1:yyyyMMdd}.log"
                                                , AppDomain.CurrentDomain.BaseDirectory, DateTime.Now);
                HttpContext   context = HttpContext.Current;
                StringBuilder sb      = new StringBuilder();
                sb.AppendFormat("异常信息:{0} ,源:{1},时间:{2}\r\n\r\n", ex.Message, ex.Source, DateTime.Now);

                sb.AppendFormat("客户IP地址:{0}\r\n", context.Request.UserHostAddress);
                sb.AppendFormat("请求地址:{0}\r\n", context.Request.Url.AbsoluteUri);
                sb.AppendFormat("堆栈信息:{0}\r\n", stackTrace);

                ChatHelper.WriteFile(fileName, sb.ToString());
            }
            catch { }
        }