Пример #1
0
 /// <summary>
 /// 带参数图片上传
 /// </summary>
 /// <param name="context"></param>
 /// <param name="collection">集合</param>
 /// <param name="pictitle">图片标题</param>
 /// <param name="t">图片处理方式</param>
 /// <param name="width">宽</param>
 /// <param name="height">高</param>
 /// <returns></returns>
 public UploadResult UseAttrUploadImg(HttpContextBase context, string collection, string pictitle = null, ResizeImgType t = ResizeImgType.Original, int width = 0, int height = 0)
 {
     var client = MongoDbServer.MongoDbClient;
     var server = client.GetServer();
     UploadResult Result = new UploadResult() { State = UploadState.Unknown };
     Result.Url = FileDomain;
     string fileExtName = null;
     double size = 0;//M
     HttpPostedFileBase file = context.Request.Files[0];
     size = file.ContentLength * 1.0 / 1024 / 1024;
     fileExtName = System.IO.Path.GetExtension(file.FileName).ToLower();
     if (string.IsNullOrEmpty(pictitle))
         pictitle = file.FileName;
     if (CustomConfig.ImgExts.Contains(fileExtName))
     {
         var tick = DateTime.Now.Ticks;
         Random rd = new Random(unchecked((int)tick));
         var imgId = tick.ToString();
         if (size < CustomConfig.CanUploadImgSize)
         {
             SunFile sunfile = new SunFile();
             sunfile.Time = DateTime.Now;
             int q = size > 0.5 ? CustomConfig.ImgQuality : 100;
             switch (t)
             {
                 case ResizeImgType.Thumbnail:
                     {
                         sunfile.Data = SunImage.GetImgThumbnail(file.InputStream, width, q, fileExtName).GetBuffer();
                     }; break;
                 case ResizeImgType.CutForCustom:
                     {
                         sunfile.Data = SunImage.CutForCustom(file.InputStream, width, height, q, fileExtName).GetBuffer();
                     }; break;
                 case ResizeImgType.CutForSquare:
                     {
                         sunfile.Data = SunImage.CutForSquare(file.InputStream, width, q, fileExtName).GetBuffer();
                     }; break;
                 default:
                     {
                         if (q!=100)
                             sunfile.Data = SunImage.ImgReduce(file.InputStream,q, fileExtName).GetBuffer();
                         else
                         {
                             sunfile.Data = new byte[file.ContentLength];
                             file.InputStream.Read(sunfile.Data, 0, sunfile.Data.Length);
                         }
                     }; break;
             }
             var imgCollection = server.GetDatabase(originalImgDatabase).GetCollection<SunFile>(collection);
             var rdId = rd.Next(99999);
             int count = 0;
             while (true)
             {
                 try
                 {
                     sunfile.Id = imgId + rdId + fileExtName;
                     imgCollection.Insert(sunfile);
                     break;
                 }
                 catch
                 {
                     if (count > 3)
                     {
                         Result.State = UploadState.FileAccessError;
                         return Result;
                     }
                     rdId = rd.Next(99999);
                 }
                 count++;
             }
             Result.Url += "/files/images/" + client.DbGroupName + "/" + collection + "/" + sunfile.Id;
             Result.OriginFileName = pictitle;
             Result.State = UploadState.Success;
             Result.FileId = sunfile.Id;
         }
         else
         {
             Result.State = UploadState.SizeLimitExceed;
         }
     }
     else { Result.State = UploadState.TypeNotAllow; }
     return Result;
 }
Пример #2
0
        /// <summary>
        /// 带参数图片上传
        /// </summary>
        /// <param name="context"></param>
        /// <param name="collection">集合</param>
        /// <param name="pictitle">图片标题</param>
        /// <param name="t">图片处理方式</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <returns></returns>
        public UploadResult UseAttrUploadImg(HttpContextBase context, string collection, string pictitle = null, ResizeImgType t = ResizeImgType.Original, int width = 0, int height = 0)
        {
            var          client = MongoDbServer.MongoDbClient;
            var          server = client.GetServer();
            UploadResult Result = new UploadResult()
            {
                State = UploadState.Unknown
            };

            Result.Url = FileDomain;
            string             fileExtName = null;
            double             size        = 0;//M
            HttpPostedFileBase file        = context.Request.Files[0];

            size        = file.ContentLength * 1.0 / 1024 / 1024;
            fileExtName = System.IO.Path.GetExtension(file.FileName).ToLower();
            if (string.IsNullOrEmpty(pictitle))
            {
                pictitle = file.FileName;
            }
            if (CustomConfig.ImgExts.Contains(fileExtName))
            {
                var    tick  = DateTime.Now.Ticks;
                Random rd    = new Random(unchecked ((int)tick));
                var    imgId = tick.ToString();
                if (size < CustomConfig.CanUploadImgSize)
                {
                    SunFile sunfile = new SunFile();
                    sunfile.Time = DateTime.Now;
                    int q = size > 0.5 ? CustomConfig.ImgQuality : 100;
                    switch (t)
                    {
                    case ResizeImgType.Thumbnail:
                    {
                        sunfile.Data = SunImage.GetImgThumbnail(file.InputStream, width, q, fileExtName).GetBuffer();
                    }; break;

                    case ResizeImgType.CutForCustom:
                    {
                        sunfile.Data = SunImage.CutForCustom(file.InputStream, width, height, q, fileExtName).GetBuffer();
                    }; break;

                    case ResizeImgType.CutForSquare:
                    {
                        sunfile.Data = SunImage.CutForSquare(file.InputStream, width, q, fileExtName).GetBuffer();
                    }; break;

                    default:
                    {
                        if (q != 100)
                        {
                            sunfile.Data = SunImage.ImgReduce(file.InputStream, q, fileExtName).GetBuffer();
                        }
                        else
                        {
                            sunfile.Data = new byte[file.ContentLength];
                            file.InputStream.Read(sunfile.Data, 0, sunfile.Data.Length);
                        }
                    }; break;
                    }
                    var imgCollection = server.GetDatabase(originalImgDatabase).GetCollection <SunFile>(collection);
                    var rdId          = rd.Next(99999);
                    int count         = 0;
                    while (true)
                    {
                        try
                        {
                            sunfile.Id = imgId + rdId + fileExtName;
                            imgCollection.Insert(sunfile);
                            break;
                        }
                        catch
                        {
                            if (count > 3)
                            {
                                Result.State = UploadState.FileAccessError;
                                return(Result);
                            }
                            rdId = rd.Next(99999);
                        }
                        count++;
                    }
                    Result.Url           += "/files/images/" + client.DbGroupName + "/" + collection + "/" + sunfile.Id;
                    Result.OriginFileName = pictitle;
                    Result.State          = UploadState.Success;
                    Result.FileId         = sunfile.Id;
                }
                else
                {
                    Result.State = UploadState.SizeLimitExceed;
                }
            }
            else
            {
                Result.State = UploadState.TypeNotAllow;
            }
            return(Result);
        }