Esempio n. 1
0
        private ResultInfo UpLoadFile(string uploadPath, ImgExtTypeEnum imgExt, bool isThumbnail = false, bool size1M = true)
        {
            ResultInfo         ri          = new ResultInfo();
            string             delFilePath = Request.Params["DelFilePath"];
            string             type        = Request.Params["itype"];
            HttpPostedFileBase upfile      = Request.Files["file"];

            if (upfile == null)
            {
                ri.Msg = "请选择要上传的文件";
                return(ri);
            }

            int aid = IsLogin ? (int)UserID : 0;

            ri = FileSaveAs(upfile, imgExt, isThumbnail, aid, uploadPath, type, size1M);

            //删除已存在的旧文件,旧文件不为空且应是上次文件,防止跨目录删除
            if (!string.IsNullOrEmpty(delFilePath) && delFilePath.IndexOf("../", StringComparison.Ordinal) == -1)
            {
                UploadHelper.DeleteUpFile(delFilePath);
            }

            return(ri);
        }
Esempio n. 2
0
        public ResultInfo UpLoadImg(string requestName, string savePath, ImgExtTypeEnum imgExt = ImgExtTypeEnum.jpg, Action <Func <ResultInfo>, ResultInfo> beforeSaveFile = null, string filename = null, bool onlyThumbImg = false, bool isNeedFile = true)
        {
            ResultInfo         ri     = new ResultInfo();
            HttpPostedFileBase upfile = Request.Files[requestName];

            if (isNeedFile)
            {
                if (upfile == null)
                {
                    ri.Msg = "请选择要上传的文件";
                }
                else
                {
                    string ext = Path.GetExtension(upfile.FileName);
                    if (!CheckFileExt(ext))
                    {
                        ri.Msg = $"不允许上传{ext}类型的文件!";
                    }
                    else
                    {
                        int maxlength = Convert.ToInt32(ConfigHelper.AppSettings("UploadFileMax"));//最大上传大小/M
                        if (upfile.ContentLength > maxlength * 1024 * 1024)
                        {
                            ri.Msg = "上传文件最大只能是{0}M".FormatWith(maxlength);
                        }
                        else
                        {
                            if (beforeSaveFile != null)
                            {
                                beforeSaveFile(() =>
                                {
                                    SaveFile(ri, upfile, savePath, ext, imgExt, filename, onlyThumbImg);
                                    return(ri);
                                }, ri);
                            }
                            else
                            {
                                SaveFile(ri, upfile, savePath, ext, imgExt, filename, onlyThumbImg);
                            }
                        }
                    }
                }
            }
            else
            {
                beforeSaveFile(null, ri);
            }
            return(ri);
        }
Esempio n. 3
0
        private void SaveFile(ResultInfo ri, HttpPostedFileBase upfile, string savePath, string ext, ImgExtTypeEnum imgExt, string filename, bool onlyThumbImg)
        {
            string localPath = UploadHelper.GetMapPath(savePath);

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

            ext = ext.ToLower().Contains("gif") ? ext : "." + imgExt.ToString();

            //如果文件名为空,则重新命名 并添加上扩展名
            if (filename.IsNullOrEmpty())
            {
                filename = DateTimeHelper.GetToday(9) + "_" + UserID + ext;
            }
            else
            {
                if (filename.IndexOf(".") == -1)
                {
                    //补全扩展名
                    filename += ext;
                }
            }
            if (onlyThumbImg)
            {
                using (Image image = Image.FromStream(upfile.InputStream))
                {
                    string thnumFilename = "thumb_{0}".FormatWith(filename);
                    string thumbFullName = localPath + "/" + thnumFilename;
                    GetThumbnail(image, 100, 100).Save(thumbFullName);
                    ri.Ok  = true;
                    ri.Url = savePath + "/" + thnumFilename;
                }
            }
            else
            {
                string fullFilePath = localPath + "/" + filename;
                upfile.SaveAs(fullFilePath);
                string picpath = savePath + "/" + filename;

                ri.Ok  = true;
                ri.Url = picpath;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 文件上传方法
        /// </summary>
        /// <param name="upfile">文件流</param>
        /// <param name="isthumbnail">是否缩略图</param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public ResultInfo FileSaveAs(HttpPostedFileBase upfile, ImgExtTypeEnum imgExt, bool isthumbnail, int aid, string uploadPath, string itype, bool size1M = true)
        {
            ResultInfo ri      = new ResultInfo();
            string     fileExt = Path.GetExtension(upfile.FileName);//文件扩展名

            //检查文件扩展名是否合法
            if (!CheckFileExt(fileExt))
            {
                ri.Msg = $"不允许上传{fileExt}类型的文件!";
                return(ri);
            }
            //检查文件大小是否合法
            //if (size1M)
            //{
            //    int maxlength = Convert.ToInt32(ConfigHelper.AppSettings("UploadFileMax"));//最大上传大小/M
            //    if (!UserBaseBLL.Instance.IsMaster && upfile.ContentLength > maxlength * 1024 * 1024)
            //    {
            //        ri.Msg = "上传文件最大只能是{0}M".FormatWith(maxlength);
            //        return ri;
            //    }
            //}

            int    fileSize       = upfile.ContentLength;                                                                                                                 //获取文件的大小,以字节为单位
            string fileName       = Path.GetFileNameWithoutExtension(upfile.FileName);                                                                                    //文件名
            string newFileName    = "{0}{1}".FormatWith(DateTimeHelper.GetToday(9), fileExt.Contains(ImgExtTypeEnum.gif.ToString()) ? fileExt : "." + imgExt.ToString()); //随机生成新的文件名
            string upLoadPath     = GetUploadPath(uploadPath, aid.ToString());                                                                                            //上传目录的相对路径
            string fullUploadPath = UploadHelper.GetMapPath(upLoadPath);                                                                                                  //上传目录的物理路径
            string newFilePath    = upLoadPath + newFileName;                                                                                                             //上传后的路径

            //检查上传的物理路径是否存在,不存在则创建
            if (!Directory.Exists(fullUploadPath))
            {
                Directory.CreateDirectory(fullUploadPath);
            }
            //保存文件
            upfile.SaveAs(fullUploadPath + newFileName);

            //生成缩略图
            string thumbFullName = string.Empty;

            if (isthumbnail)
            {
                using (Image image = Image.FromStream(upfile.InputStream))
                {
                    string thumbPath     = GetUploadPath(GetUploadPath(uploadPath, "thumb"), aid.ToString());
                    string thumbFullPath = UploadHelper.GetMapPath(thumbPath);
                    if (!Directory.Exists(thumbFullPath))
                    {
                        Directory.CreateDirectory(thumbFullPath);
                    }
                    thumbFullName = thumbPath + "thumb_{0}".FormatWith(newFileName);
                    GetThumbnail(image, 100, 100).Save(Server.MapPath(thumbFullName));
                }
            }

            //处理完毕,返回JOSN格式的文件信息
            ri.Ok   = true;
            ri.Msg  = "上传文件成功!";
            ri.Url  = newFilePath;
            ri.Data = thumbFullName;
            return(ri);
            //return new { status = 1, msg = "上传文件成功!", path = newFilePath, thumbName = thumbFullName, size = fileSize, ext = fileExt };
        }