예제 #1
0
        // GET: /Source/
        /// <summary>
        /// 上传一个文件
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <param name="maxSize">接受的最大的文件大小</param>
        /// <param name="filters">可接受的扩展名</param>
        /// <param name="dirPath">要保存的路径(路径即可,文件名是自动生成的,注意路径最后要加斜杠)</param>
        /// <returns></returns>
        private JsonResult UploadFile(HttpPostedFileBase file, Array filters, int maxSize, string dirPath)
        {
            //FileCommon.ExistsCreate(dirPath);
            if (file == null)
            {
                return(UploadResult(1, "没有文件"));
            }
            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                return(UploadResult(1, "上传文件大小超过限制"));
            }
            //file.InputStream可以获取到System.io.Stream对象,由此可以对文件进行hash加密运算
            string fileName = file.FileName,
                   fileExt  = Path.GetExtension(fileName).ToLower();//扩展名

            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(filters, fileExt.Substring(1).ToLower()) == -1)
            {
                return(UploadResult(1, "上传文件扩展名是不允许的扩展名"));
            }
            string newFileName = string.Empty, //新生成的文件名
                   filePath    = string.Empty;

            if (string.IsNullOrEmpty(dirPath))
            {
                return(UploadResult(1, "服务器异常"));
            }
            newFileName = FileCommon.CreateFileNameByTime() + fileExt;
            filePath    = dirPath + newFileName;
            file.SaveAs(Server.MapPath(filePath));

            //分析上传的文件信息,返回解析得到的结果
            return(UploadResult(0, "上传成功", newFileName));
        }
예제 #2
0
        /// <summary>
        /// 验证一篇said是否是有效的said,同时矫正Said的数据
        /// </summary>
        /// <param name="model">要验证的model</param>
        /// <returns>返回null表示验证成功,否则返回验证失败的字符串,用,号分割</returns>
        public string ValidateAndCorrectSubmit(Article model, SongApplication songApplication, ImageApplication imageApplication)
        {
            StringBuilder str = new StringBuilder();

            //防止tag有HTML标签,修正
            if (!string.IsNullOrWhiteSpace(model.STag))
            {
                model.STag = HTMLCommon.HTMLTrim(model.STag);
            }
            //model.Song = SongApplication.Context.GetById(model.SongId);//检索有没有歌曲信息
            if (songApplication.FindById(model.SongId) == null)
            {
                str.Append("歌曲信息不正确(不可获取),");
            }
            if (imageApplication.FindById(model.ImageId) == null)
            {
                str.Append("缩略图信息不正确(不可获取),");
            }

            foreach (var validateResult in model.Validate())
            {
                //validateResult.MemberNames//这个要搞懂怎么用,或许能让提示信息更全一点
                str.Append(validateResult.ErrorMessage + ",");
            }
            if (str.Length > 0)
            {
                str.Length--;//StringBuilder的length可以用于裁剪字符串?
            }
            else
            {
                if (string.IsNullOrEmpty(model.SaidId))
                {
                    //新增
                    if (string.IsNullOrWhiteSpace(model.SName) || FindByFileName(model.SName.Trim()) != null)    //没有文件名或文件名不合法,则生成一个新的文件名
                    {
                        model.SName = FileCommon.CreateFileNameByTime();
                    }
                }
                else
                {
                    //编辑
                    if (string.IsNullOrWhiteSpace(model.SName))
                    {
                        model.SName = FileCommon.CreateFileNameByTime();
                    }
                    else
                    {
                        //从数据库中检索是否存在
                        var SaidIdLists = FindSaidIdByFileName(model.SName).ToList();
                        //文件名重复
                        if (SaidIdLists.Count > 1 && SaidIdLists.IndexOf(model.SaidId) > -1)
                        {
                            model.SName = FileCommon.CreateFileNameByTime();
                        }
                    }
                }
            }

            return(str.Length > 0 ? str.ToString() : null);
        }
예제 #3
0
        /// <summary>
        /// 验证一篇blog是否是有效的blog,同时矫正blog的数据
        /// </summary>
        /// <param name="model">要验证的model</param>
        /// <returns>返回null表示验证成功,否则返回验证失败的字符串,用,号分割</returns>
        public string ValidateAndCorrectSubmit(Blog model, ClassifyApplication classifyContext)
        {
            StringBuilder str = new StringBuilder();

            //防止tag有HTML标签,修正
            foreach (var validateResult in model.Validate())
            {
                //validateResult.MemberNames//这个要搞懂怎么用,或许能让提示信息更全一点
                str.Append(validateResult.ErrorMessage + ",");
            }
            if (classifyContext.FindById(model.ClassifyId) == null)
            {
                str.Append("分类信息不正确,");
            }
            if (str.Length > 0)
            {
                str.Length--;//StringBuilder的length可以用于裁剪字符串?
            }
            else
            {
                if (string.IsNullOrEmpty(model.BlogId))//新增
                {
                    //开始矫正数据
                    //没有文件名或文件名不合法,则生成一个新的文件名
                    if (string.IsNullOrWhiteSpace(model.BName) || FindByFileName(model.BName.Trim()) != null)
                    {
                        model.BName = FileCommon.CreateFileNameByTime();
                    }
                }
                else
                {
                    //编辑
                    if (string.IsNullOrWhiteSpace(model.BName))
                    {
                        model.BName = FileCommon.CreateFileNameByTime();
                    }
                    else
                    {
                        //从数据库中检索是否存在
                        var SaidIdLists = FindBlogIdByFileName(model.BName).ToList();
                        //文件名重复
                        if (SaidIdLists.Count > 1 && SaidIdLists.IndexOf(model.BName) > -1)
                        {
                            model.BName = FileCommon.CreateFileNameByTime();
                        }
                    }
                }
            }
            return(str.Length > 0 ? str.ToString() : null);
        }
예제 #4
0
        /// <summary>
        /// 添加Blog,会自动修正Blog的数据,新增blogId
        /// </summary>
        /// <param name="blog"></param>
        /// <param name="tags"></param>
        /// <returns></returns>
        public void AddBlog(Blog blog, IList <Tag> tags, BlogTagsApplication blogTagsApplication, TagApplication tagApplication)
        {
            //先调用ValidateAndCorrectSubmit验证更合理
            blog.BlogId = string.IsNullOrWhiteSpace(blog.BName) ? FileCommon.CreateFileNameByTime() : blog.BName;
            blog.Date   = DateTime.Now;
            IList <BlogTags> blogTags = blogTagsApplication.UpdateBlogTags(blog, tags, tagApplication);

            Add(blog);
            if (!Commit())
            {
                throw new Exception("新增Blog异常");
            }
            //新增BlogTags完毕,新增Blog
            blogTagsApplication.AddLists(blogTags);
        }
예제 #5
0
        /// <summary>
        /// 上传一个图片,保存并返回图片信息新生成的文件名
        /// </summary>
        /// <param name="file"></param>
        /// <param name="filters"></param>
        /// <param name="maxSize"></param>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        private Dictionary <string, string> Save(HttpPostedFileBase file, Array filters, int maxSize, string dirPath)
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            dirPath = Server.MapPath(dirPath);
            //FileCommon.ExistsCreate(dirPath);
            if (file == null)
            {
                result.Add("code", "1");
                result.Add("msg", "没有文件");
                return(result);
            }
            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                result.Add("code", "1");
                result.Add("msg", "上传文件大小超过限制");
                return(result);
            }
            //file.InputStream可以获取到System.io.Stream对象,由此可以对文件进行hash加密运算
            string fileName = file.FileName,
                   fileExt  = Path.GetExtension(fileName).ToLower();//扩展名

            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(filters, fileExt.Substring(1).ToLower()) == -1)
            {
                result.Add("code", "1");
                result.Add("msg", "上传文件扩展名是不允许的扩展名");
                return(result);
            }
            string newFileName = string.Empty, //新生成的文件名
                   filePath    = string.Empty;

            if (string.IsNullOrEmpty(dirPath))
            {
                result.Add("code", "1");
                result.Add("msg", "服务器异常");
                return(result);
            }
            newFileName = FileCommon.CreateFileNameByTime() + fileExt;
            filePath    = dirPath + newFileName;
            file.SaveAs(Server.MapPath(filePath));
            result.Add("code", "0");
            result.Add("path", filePath);
            result.Add("dir", dirPath);
            result.Add("name", newFileName);
            return(result);
        }
예제 #6
0
        /// <summary>
        /// 上传一个图片,保存并返回图片信息新生成的文件名
        /// </summary>
        /// <param name="file"></param>
        /// <param name="filters"></param>
        /// <param name="maxSize"></param>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        private Dictionary <string, string> Save(HttpPostedFileBase file, Array filters, int maxSize, string dirPath)
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            dirPath = Server.MapPath(dirPath);
            //FileCommon.ExistsCreate(dirPath);
            if (file == null)
            {
                result.Add("code", "1");
                result.Add("msg", "没有文件");
                return(result);
            }
            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                result.Add("code", "1");
                result.Add("msg", "上传文件大小超过限制");
                return(result);
            }
            //file.InputStream可以获取到System.io.Stream对象,由此可以对文件进行hash加密运算
            string fileName = file.FileName,
                   fileExt  = Path.GetExtension(fileName).ToLower();//扩展名

            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(filters, fileExt.Substring(1).ToLower()) == -1)
            {
                result.Add("code", "1");
                result.Add("msg", "上传文件扩展名是不允许的扩展名");
                return(result);
            }
            string newFileName = string.Empty, //新生成的文件名
                   filePath    = string.Empty;

            if (string.IsNullOrEmpty(dirPath))
            {
                result.Add("code", "1");
                result.Add("msg", "服务器异常");
                return(result);
            }


            newFileName = FileCommon.CreateFileNameByTime() + fileExt;
            filePath    = dirPath + newFileName;
            file.SaveAs(filePath);

            //TODO 这里需要检测有没有什么异常
            MusicInfo music = MusicCommon.GetFileInfo(filePath);

            music.Size = file.ContentLength;
            music.Type = fileExt.Substring(1);
            //if (string.IsNullOrEmpty(music.Length))
            //{
            //    //测试了10个左右的文件,发现木有音乐文件长度不存在的情况
            //    if (music.BitRate == 0)
            //    {
            //        result.Add("code", "2");
            //        result.Add("msg", "文件异常(无法读取到正确的文件信息)");
            //        return result;
            //    }
            //    else
            //    {
            //        //尝试转换
            //        //这里得到的是字节么?
            //        music.Length = (music.Size * 1024 / music.BitRate * 8).ToString();  //歌曲时长 = 文件大小(mb)/ 位率(KBPS) * 8
            //        System.Diagnostics.Debug.WriteLine(music.Length);
            //    }
            //}


            result.Add("code", "0");
            result.Add("path", filePath);
            result.Add("dir", dirPath);
            result.Add("name", newFileName);
            result.Add("data", JavaScriptCommon.Serialize(music));
            return(result);
        }