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; } }
/// <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 }; }