Esempio n. 1
0
        private static void LogToExceptionFile(List <LogInfo> exceptions)
        {
            StringBuilder sb = new StringBuilder();

            foreach (LogInfo logInfo in exceptions)
            {
                sb.AppendFormat("{0}\t{1}\t{2}\t\r\n{3}\r\n", logInfo.LogTime, logInfo.ThreadId, ((Exception)logInfo.Message).Message, ((Exception)logInfo.Message).StackTrace);
            }
            if (_exceptionWriter == null)
            {
                string file =
                    FilePathUtil.GetMapPath("/log/" + DateTime.Now.ToString("yyyyMMdd") + "ex." +
                                            LogFileExtendName);
                FileInfo fileInfo = new FileInfo(file);
                if (!fileInfo.Directory.Exists)
                {
                    fileInfo.Directory.Create();
                }
                _exceptionWriter = new global::System.IO.StreamWriter(file, true, Encoding.UTF8);
            }
            _exceptionWriter.Write(sb.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// 上传并保存文件 表单别忘记加 enctype="multipart/form-data"
        /// </summary>
        /// <param name="savePath">保存路径 请以/结尾</param>
        /// <param name="supportExts">支持的扩展名 |分割 如:JPG|GIF ,留空上传任意文件</param>
        /// <param name="useOriginalFileName">使用上传的文件名</param>
        /// <param name="overwite">同名文件是否覆盖</param>
        /// <returns></returns>
        public static IList <string> UploadFiles(string savePath, string supportExts, bool useOriginalFileName, bool overwite)
        {
            if (!string.IsNullOrEmpty(supportExts))
            {
                for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
                {
                    HttpPostedFile file    = HttpContext.Current.Request.Files[i];
                    string         extname = FilePathUtil.GetFileExtName(file.FileName);
                    if (string.IsNullOrEmpty(extname))
                    {
                        throw new Exception(string.Format("{0} 文件扩展名不存在", file.FileName));
                    }
                    if (!Utils.InArray(extname.Trim('.'), supportExts, "|", true))
                    {
                        throw new SecurityException(string.Format("没权限上传 {0} 格式的文件 ", extname));
                    }
                }
            }
            string path = savePath;

            if (!path.Contains(":"))
            {
                path = FilePathUtil.GetMapPath(savePath);
            }
            IList <string> list = new List <string>();

            for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
            {
                HttpPostedFile file     = HttpContext.Current.Request.Files[i];
                string         extname  = FilePathUtil.GetFileExtName(file.FileName);
                string         filename = "";
                if (useOriginalFileName)
                {
                    filename = path + file.FileName.Substring(file.FileName.Replace("\\", "/").LastIndexOf("/") + 1);
                }
                else
                {
                    filename = path + DateTime.Now.ToString("yyyyMMddHHmmss") + RNG.Next(10000, 99999) + extname;
                }

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                if (FilePathUtil.FileExists(filename))
                {
                    if (overwite)
                    {
                        File.Delete(filename);
                    }
                    else
                    {
                        throw new Exception(string.Format("{0} 已经存在", filename));
                    }
                }

                file.SaveAs(filename);
                list.Add(filename);
            }

            return(list);
        }