Ejemplo n.º 1
0
        /// <summary>
        ///  写文本日志
        /// </summary>
        /// <param name="txtinfo">文本文件内容</param>
        /// <param name="txtfilepath">文本文件路径</param>
        /// <param name="txtfilename">文本文件名称</param>
        public static void WriteLog(string txtinfo, string txtfilepath, string txtfilename)
        {
            FileStream   fs = null;
            StreamWriter sw = null;

            try
            {
                txtfilename = string.IsNullOrEmpty(txtfilename) ? DateTime.Now.ToString("yyyyMMdd") + ".txt" : txtfilename + ".txt";
                string dirpath = AppDomain.CurrentDomain.BaseDirectory + txtfilepath;
                SimonUtils.CreateDir(dirpath);
                fs = new FileStream(dirpath + txtfilename, System.IO.FileMode.Append, System.IO.FileAccess.Write);
                sw = new StreamWriter(fs, Encoding.UTF8);
                sw.WriteLine(DateTime.Now.ToString() + "     " + txtinfo + "\r\n");
            }
            finally
            {
                if (sw != null)
                {
                    sw.Flush();
                    sw.Dispose();
                    sw = null;
                }
                if (fs != null)
                {
                    fs.Dispose();
                    fs = null;
                }
            }
        }
Ejemplo n.º 2
0
    /// <summary>
    /// 文件上传方法
    /// </summary>
    /// <param name="postedFile">文件流</param>
    /// <param name="isThumbnail">是否生成缩略图</param>
    /// <param name="isWater">是否打水印</param>
    /// <returns>上传后文件信息</returns>
    public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater)
    {
        try
        {
            string fileExt              = SimonUtils.GetFileExt(postedFile.FileName);                               //文件扩展名,不含“.”
            int    fileSize             = postedFile.ContentLength;                                                 //获得文件大小,以字节为单位
            string fileName             = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名
            string newFileName          = SimonUtils.GetRamCode() + "." + fileExt;                                  //随机生成新的文件名
            string newThumbnailFileName = "thumb_" + newFileName;                                                   //随机生成缩略图文件名
            string upLoadPath           = GetUpLoadPath();                                                          //上传目录相对路径
            string fullUpLoadPath       = SimonUtils.GetMapPath(upLoadPath);                                        //上传目录的物理路径
            string newFilePath          = upLoadPath + newFileName;                                                 //上传后的路径
            string newThumbnailPath     = upLoadPath + newThumbnailFileName;                                        //上传后的缩略图路径

            //检查文件扩展名是否合法
            if (!CheckFileExt(fileExt))
            {
                return("不允许上传" + fileExt + "类型的文件");
            }
            //检查文件大小是否合法
            if (!CheckFileSize(fileExt, fileSize))
            {
                return("文件超过限制的大小");
            }
            //检查上传的物理路径是否存在,不存在则创建
            SimonUtils.CreateDir(fullUpLoadPath);

            //保存文件
            postedFile.SaveAs(fullUpLoadPath + newFileName);
            //如果是图片,检查图片是否超出最大尺寸,是则裁剪
            if (IsImage(fileExt) && (Upload_PicMaxWidth > 0 || Upload_PicMaxHeight > 0))
            {
                SimonThumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName,
                                                  Upload_PicMaxWidth, Upload_PicMaxHeight);
            }
            //如果是图片,检查是否需要生成缩略图,是则生成
            if (IsImage(fileExt) && isThumbnail && Upload_PicThumbnailWidth > 0 && Upload_PicThumbnailHeight > 0)
            {
                SimonThumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName,
                                                  Upload_PicThumbnailWidth, Upload_PicThumbnailHeight, "Cut");
            }
            //如果是图片,检查是否需要打水印
            if (IsWaterMark(fileExt) && isWater)
            {
                switch (Upload_PicWaterMarkType)
                {
                case 1:
                    SimonWaterMark.AddImageSignText(newFilePath, newFilePath, Upload_PicWaterMark_Txt, 9, 63, "黑体", 16);
                    break;

                case 2:
                    SimonWaterMark.AddImageSignPic(newFilePath, newFilePath, Upload_PicWaterMark_PicUrl, 9, 63, 6);
                    break;
                }
            }
            //处理完毕,返回JOSN格式的文件信息
            //return "{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \""
            //    + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \""
            //    + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}";
            return("success|" + newFilePath + "");
        }
        catch
        {
            return("上传过程中发生意外错误");
        }
    }