示例#1
0
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="files">上传图片集合</param>
        /// <param name="OriginalKey">原图路径</param>
        /// <param name="FileKey">缩略图路径</param>
        /// <param name="SmallKey">小图路径</param>
        /// <returns>上传之后的路径集合</returns>
        public List <FileClass> UploadImages(HttpFileCollectionBase files, string OriginalKey, string FileKey, string SmallKey)
        {
            List <FileClass> list_fc = new List <FileClass>();

            foreach (string fName in files)
            {
                HttpPostedFileBase file = files[fName];
                FileClass          fc   = new FileClass();
                if (file == null || file.ContentLength <= 0)
                {
                    continue;
                }

                DateTime dt           = DateTime.Now;
                string   OriginalPath = OriginalKey;

                #region 创建文件夹
                //原图
                if (!Directory.Exists(OriginalPath))
                {
                    Directory.CreateDirectory(OriginalPath);
                }
                string OriginalPathYear = OriginalKey + "\\" + dt.Year;
                if (!Directory.Exists(OriginalPathYear))
                {
                    Directory.CreateDirectory(OriginalPathYear);
                }
                string OriginalPathdate = OriginalPathYear + "\\" + dt.ToString("yyyyMMdd");
                if (!Directory.Exists(OriginalPathdate))
                {
                    Directory.CreateDirectory(OriginalPathdate);
                }
                //缩略图
                string FilePath = FileKey;
                if (string.IsNullOrEmpty(FilePath))
                {
                    FilePath = OriginalPath;
                }
                if (!Directory.Exists(FilePath))
                {
                    Directory.CreateDirectory(FilePath);
                }
                string FilePathYear = FilePath + "\\" + dt.Year;
                if (!Directory.Exists(FilePathYear))
                {
                    Directory.CreateDirectory(FilePathYear);
                }
                string FilePathdate = FilePathYear + "\\" + dt.ToString("yyyyMMdd");
                if (!Directory.Exists(FilePathdate))
                {
                    Directory.CreateDirectory(FilePathdate);
                }
                //小图
                string SmallPath = SmallKey;
                if (string.IsNullOrEmpty(SmallPath))
                {
                    SmallPath = OriginalPath;
                }
                if (!Directory.Exists(SmallPath))
                {
                    Directory.CreateDirectory(SmallPath);
                }
                string SmallPathYear = SmallPath + "\\" + dt.Year;
                if (!Directory.Exists(SmallPathYear))
                {
                    Directory.CreateDirectory(SmallPathYear);
                }
                string SmallPathdate = SmallPathYear + "\\" + dt.ToString("yyyyMMdd");
                if (!Directory.Exists(SmallPathdate))
                {
                    Directory.CreateDirectory(SmallPathdate);
                }
                #endregion

                string timeStr = dt.ToString("yyyyMMddHHmmssffff") + new Random().Next(10000, 99999);

                string fileName = timeStr + Path.GetExtension(file.FileName);

                string OfileName = Path.GetFileName(file.FileName);

                //防止用户上传非图片文件
                //文件类型
                string fileType = Path.GetExtension(file.FileName);
                if (fileType == ".jpg" ||
                    fileType == ".bmp" ||
                    fileType == ".gif" ||
                    fileType == ".png")
                {
                    string OPath = Path.Combine(OriginalPathdate, fileName);
                    file.SaveAs(OPath);//保存原图

                    fc.OriginalPath = dt.Year + "/" + dt.ToString("yyyyMMdd") + "/" + fileName;
                    fc.OriginalName = OfileName;
                    fc.OriginalType = fileType;


                    //string FPath = Path.Combine(FilePathdate, fileName);


                    //int OH = 800;
                    //int OW = 600;

                    //FileFactory.MakeThumbnail(OPath, FPath, OW, OH, "HW");//保存缩略图
                    //fc.FilesPath = dt.Year + "/" + dt.ToString("yyyyMMdd") + "/" + fileName;
                    //fc.FilesName = OfileName;
                    //fc.FilesType = fileType;

                    string SPath = Path.Combine(SmallPathdate, fileName);


                    int SH = 100;
                    int SW = 100;

                    FileFactory.MakeThumbnail(OPath, SPath, SW, SH, "HW");//保存小图
                    fc.SmallPath = dt.Year + "/" + dt.ToString("yyyyMMdd") + "/" + fileName;
                    fc.SmallName = OfileName;
                    fc.SmallType = fileType;

                    list_fc.Add(fc);
                }
            }
            return(list_fc);
        }
示例#2
0
文件: FileHelper.cs 项目: zkg642/JXXZ
        /// <summary>
        /// 附件上传
        /// </summary>
        /// <param name="file">文件域</param>
        /// <param name="OriginalKey">原图路径</param>
        /// <param name="thumbnailKey">缩略图路径</param>
        /// <returns></returns>
        public static FileUploadClass UploadFile(HttpPostedFileBase file, string OriginalKey, int Fheight = 48, int Fwidth = 48)
        {
            FileUploadClass fc = new FileUploadClass();

            try
            {
                DateTime dt = DateTime.Now;

                string OriginalPath = OriginalKey;

                string fileType = Path.GetExtension(file.FileName);

                //原图 创建文件夹
                if (!Directory.Exists(OriginalPath))
                {
                    Directory.CreateDirectory(OriginalPath);
                }
                string OriginalPathYear = OriginalKey + "\\" + dt.Year;
                if (!Directory.Exists(OriginalPathYear))
                {
                    Directory.CreateDirectory(OriginalPathYear);
                }
                string OriginalPathdate = OriginalPathYear + "\\" + dt.ToString("yyyyMMdd");
                if (!Directory.Exists(OriginalPathdate))
                {
                    Directory.CreateDirectory(OriginalPathdate);
                }

                string FilePathdate = string.Empty;

                if (fileType.Contains(".jpg") ||
                    fileType.Contains(".bmp") ||
                    fileType.Contains(".gif") ||
                    fileType.Contains(".png"))
                {
                    //缩略图
                    string FilePath = OriginalKey + "thumbnailpath\\";
                    if (string.IsNullOrEmpty(FilePath))
                    {
                        FilePath = OriginalPath;
                    }
                    if (!Directory.Exists(FilePath))
                    {
                        Directory.CreateDirectory(FilePath);
                    }
                    string FilePathYear = FilePath + "\\" + dt.Year;
                    if (!Directory.Exists(FilePathYear))
                    {
                        Directory.CreateDirectory(FilePathYear);
                    }
                    FilePathdate = FilePathYear + "\\" + dt.ToString("yyyyMMdd");
                    if (!Directory.Exists(FilePathdate))
                    {
                        Directory.CreateDirectory(FilePathdate);
                    }
                }


                string timeStr = dt.ToString("yyyyMMddHHmmssffff") + new Random().Next(10000, 99999);

                string fileName = timeStr + Path.GetExtension(file.FileName);

                string OfileName = Path.GetFileName(file.FileName);

                string OPath = Path.Combine(OriginalPathdate, fileName);
                file.SaveAs(OPath);//保存原图

                if (!string.IsNullOrEmpty(FilePathdate))
                {
                    string FPath = Path.Combine(FilePathdate, fileName);
                    FileFactory.MakeThumbnail(OPath, FPath, Fheight, Fwidth, "HW");//保存缩略图
                }

                fc.OriginalPath = dt.Year + "/" + dt.ToString("yyyyMMdd") + "/" + fileName;
                fc.OriginalName = OfileName;
                fc.OriginalType = fileType;
                fc.size         = file.ContentLength / 1024;
                fc.success      = true;
            }
            catch (Exception)
            {
                fc.success = false;
            }
            return(fc);
        }