}//SaveFile

        private static void AttachToTopic(string topicID, GarfielderEntities db, XFile file)
        {
            //refID proccess
            var objID = Guid.Parse(topicID);

            if (objID == Guid.Empty)
            {
                return;
            }
            var obj = file.Topics.SingleOrDefault(x => x.Id == objID);

            //exists
            if (obj != null)
            {
                return;
            }
            //attach
            file.Topics.Add(db.Topics.Single(x => x.Id.Equals(objID)));
            db.SaveChanges();
        }
        /// <summary>
        /// Save current file uploaded
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public static VMXFileEdit SaveFile(HttpRequestBase req)
        {
            var vm = new VMXFileEdit();

            vm.NoFlash = true;


            if (req.Files.Count == 0 || req.Files[0].FileName == "" || req.Files[0].ContentLength == 0)
            {
                vm.Error = true;
                vm.Msg   = "No file selected!Please select a file to upload!";
                return(vm);
            }
            ;
            var refid    = req.Params["RefId"];
            var uid      = req.Params["UserID"];
            var uname    = req.Params["UserName"];
            var fileName = req.Files[0].FileName.CHSToPinyin("-").ToLower();
            var path0    = getFileName(uname, fileName);                                           //虚拟路径
            var path1    = Path.Combine(_uploadsFolder, uname, DateTime.Now.ToString("yyyyMMdd")); //物理路径

            //db op
            using (var db = new GarfielderEntities())
            {
                var dbm = db.XFiles.SingleOrDefault(x => x.Name == path0);
                if (dbm == null)
                {
                    //save to disk
                    vm.Name = path0;

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

                    try
                    {
                        vm.Title       = req.Files[0].FileName;
                        vm.Description = vm.Title;
                        vm.Extension   = vm.Name.Substring(vm.Name.LastIndexOf("."));

                        vm.Id        = Guid.NewGuid();
                        vm.CreatedAt = DateTime.Now;

                        path1 = string.Format("{0}\\{1}", path1, fileName);
                        req.Files[0].SaveAs(path1);

                        //图片缩略图处理);
                        if (IsImg(vm.Extension))
                        {
                            //大图800x600
                            var tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.L800X600), 800, 600, true);
                            vm.MetaData.Width  = tempObj.RawSize.Width;
                            vm.MetaData.Height = tempObj.RawSize.Height;

                            //原图
                            vm.MetaData.AddThumb(ImageFlags.RAW,
                                                 string.Format("{0}{1}",
                                                               Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name),
                                                 vm.MetaData.Width, vm.MetaData.Height);

                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.L800X600,
                                                     string.Format("{0}{1}_800x600{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            //中大图500x500
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.L500X500), 500, 500, true);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.L500X500,
                                                     string.Format("{0}{1}_500x500{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            //中
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.M300X300), 300, 300, true);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.M300X300,
                                                     string.Format("{0}{1}_300x300{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            //中小图
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.M160X160), 160, 160, true);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.M160X160,
                                                     string.Format("{0}{1}_160x160{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.S160X100), 160, 100);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.S160X100,
                                                     string.Format("{0}{1}_160x100{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            //小
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.S64X64), 64, 64);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.S64X64,
                                                     string.Format("{0}{1}_64x64{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                        }
                        ; //IsImg

                        dbm             = new XFile();
                        dbm.Id          = vm.Id;
                        dbm.Name        = path0;
                        dbm.Url         = path0;
                        dbm.Title       = vm.Title;
                        dbm.Description = vm.Description;
                        dbm.Extension   = vm.Extension;
                        dbm.CreatedAt   = dbm.ModifiedAt = vm.CreatedAt;
                        dbm.Meta        = vm.Meta;

                        dbm.UserID        = Guid.Parse(uid);
                        db.CommandTimeout = 0;
                        db.AddToXFiles(dbm);
                        db.SaveChanges();
                        ClearCache();
                        //attach to topic
                        AttachToTopic(refid, db, dbm);
                    }
                    catch (Exception ex)
                    {
                        //TODO:log
                        vm.Error = true;
                        vm.Msg   = ex.Message;
                    }//TRY
                }
                else
                {
                    //FILE Exists
                    vm.Error = true;
                    vm.Msg   = string.Format("File {0} exists!", path0);
                    //attach to topic
                    AttachToTopic(refid, db, dbm);
                };
            };//using
            return(vm);
        }//SaveFile