/// <summary> /// 文件上传 /// </summary> /// <param name="collection"></param> /// <returns></returns> public UploadResult UploadFile(HttpContextBase context,string collection) { var server = MongoDbServer.MongoDbClient; HttpPostedFileBase file = context.Request.Files[0]; UploadResult Result = new UploadResult() { State = UploadState.Unknown }; var Size = file.ContentLength * 1.0 / 1024 / 1024; var extName = System.IO.Path.GetExtension(file.FileName).ToLower(); Result.Url = FileDomain; Result.OriginFileName=file.FileName; if (CustomConfig.FileExts.Contains(extName)) { Random rd = new Random(); var fileId = DateTime.Now.Ticks + rd.Next(9999) + extName; if (Size < CustomConfig.CanUploadFileSize) { if (Size <= 6) { SunFile sunfile = new SunFile(); byte[] data = new byte[file.ContentLength]; file.InputStream.Read(data, 0, data.Length); sunfile.Id = fileId; sunfile.Data = data; sunfile.Time = DateTime.Now; var imgCollection = server.GetDatabase(fileDatabase).GetCollection<SunFile>(collection); imgCollection.InsertOneAsync(sunfile); Result.Url += "/files/SmallFile/" + server.DbGroupName + "/" + collection + "/" + fileId; Result.State = UploadState.Success; } else { MongoGridFS gfs = new MongoGridFS(server.GetServer(), fileDatabase, new MongoGridFSSettings()); gfs.Upload(file.InputStream, fileId); Result.Url += "/files/LargeFile/" + server.DbGroupName + "/" + fileId; Result.State = UploadState.Success; } Result.FileId = fileId; } else { Result.State = UploadState.SizeLimitExceed; } } else { Result.State = UploadState.TypeNotAllow; } return Result; }
/// <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, CutImageType t = CutImageType.Original, int width = 0, int height = 0) { var server = MongoDbServer.MongoDbClient; 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 CutImageType.Thumbnail: { sunfile.Data = SunImage.GetImgThumbnail(file.InputStream, width, q, fileExtName).GetBuffer(); }; break; case CutImageType.CutForCustom: { sunfile.Data = SunImage.CutForCustom(file.InputStream, width, height, q, fileExtName).GetBuffer(); }; break; case CutImageType.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.InsertOneAsync(sunfile); break; } catch { if (count > 3) { Result.State = UploadState.FileAccessError; return Result; } rdId = rd.Next(99999); } count++; } Result.Url += "/files/images/" + server.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; }
public UploadResult Upload(UploadConfig config) { UploadResult Result = new UploadResult() { State = UploadState.Unknown }; byte[] uploadFileBytes = null; string uploadFileName = null; string fileExt = null; if (config.Base64) { uploadFileName = config.Base64Filename; fileExt = ".png"; uploadFileBytes = Convert.FromBase64String(Request[config.UploadFieldName]); } else { var file = Request.Files[0]; uploadFileName = file.FileName; fileExt = Path.GetExtension(uploadFileName).ToLower(); if (!config.AllowExtensions.Contains(fileExt)) { Result.State = UploadState.TypeNotAllow; return Result; } if (file.ContentLength > config.SizeLimit) { Result.State = UploadState.SizeLimitExceed; return Result; } uploadFileBytes = new byte[file.ContentLength]; try { file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } catch (Exception) { Result.State = UploadState.NetworkError; return Result; } } Result.OriginFileName = uploadFileName; var savePath = config.PathFormat + DateTime.Now.ToString("yyyy/MM/"); savePath += Guid.NewGuid() + fileExt; var localPath = Server.MapPath(savePath); try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } File.WriteAllBytes(localPath, uploadFileBytes); Result.Url = savePath; Result.State = UploadState.Success; } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } return Result; }
/// <summary> /// mongodb图片上传 /// </summary> /// <param name="context"></param> /// <param name="collection">集合</param> /// <param name="pictitle">图片标题</param> /// <param name="base64">是否是以base64上传的</param> /// <param name="inputName">图片表单域名称</param> /// <returns>上传结果</returns> public UploadResult UploadImg(HttpContextBase context, string collection, string pictitle = null, bool base64 = false, string inputName = "upfile") { var server = MongoDbServer.MongoDbClient; UploadResult Result = new UploadResult() { State = UploadState.Unknown }; Result.Url = FileDomain; byte[] fileData = null; string fileExtName = null; double size = 0;//M if (base64) { fileExtName = ".png"; if (string.IsNullOrEmpty(pictitle)) pictitle = inputName + fileExtName; fileData = Convert.FromBase64String(context.Request[inputName]); size = fileData.Length * 1.0 / 1024 / 1024; } else { 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; fileData = new byte[file.ContentLength]; file.InputStream.Read(fileData, 0, fileData.Length); } 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; if (size > 0.5) sunfile.Data = SunImage.ImgReduce(new MemoryStream(fileData), CustomConfig.ImgQuality, fileExtName).GetBuffer(); else sunfile.Data = fileData; 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.InsertOneAsync(sunfile); break; } catch { if (count > 3) { Result.State = UploadState.FileAccessError; return Result; } rdId = rd.Next(99999); } count++; } Result.Url += "/files/images/" + server.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; }
/// <summary> /// 使用参数上传图片 /// </summary> /// <param name="config">配置</param> /// <param name="defaultQuality">默认图片质量(1-100)</param> /// <param name="t">图片处理方式</param> /// <param name="width">宽</param> /// <param name="height">高</param> /// <returns></returns> public UploadResult UseAttrUploadImage(UploadConfig config, int defaultQuality, CutImageType t = CutImageType.Original, int width = 0, int height = 0) { UploadResult Result = new UploadResult() { State = UploadState.Unknown }; byte[] uploadFileBytes = null; string uploadFileName = null; string fileExt = null; if (config.Base64) { uploadFileName = config.Base64Filename; fileExt = ".png"; uploadFileBytes = Convert.FromBase64String(Request[config.UploadFieldName]); } else { var file = Request.Files[0]; uploadFileName = file.FileName; fileExt = Path.GetExtension(uploadFileName).ToLower(); if (!config.AllowExtensions.Contains(fileExt)) { Result.State = UploadState.TypeNotAllow; return Result; } if (file.ContentLength > config.SizeLimit) { Result.State = UploadState.SizeLimitExceed; return Result; } try { int q = file.ContentLength/1024/1024 > 0.5 ? defaultQuality : 100; switch (t) { case CutImageType.Thumbnail: { uploadFileBytes = SunImage.GetImgThumbnail(file.InputStream, width, q, fileExt).GetBuffer(); }; break; case CutImageType.CutForCustom: { uploadFileBytes = SunImage.CutForCustom(file.InputStream, width, height, q, fileExt).GetBuffer(); }; break; case CutImageType.CutForSquare: { uploadFileBytes = SunImage.CutForSquare(file.InputStream, width, q, fileExt).GetBuffer(); }; break; default: { if (q != 100) uploadFileBytes = SunImage.ImgReduce(file.InputStream, q, fileExt).GetBuffer(); else { uploadFileBytes = new byte[file.ContentLength]; file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } }; break; } } catch (Exception) { Result.State = UploadState.NetworkError; return Result; } } Result.OriginFileName = uploadFileName; var savePath = config.PathFormat + DateTime.Now.ToString("yyyy/MM/"); savePath += Guid.NewGuid() + fileExt; var localPath = Server.MapPath(savePath); try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } File.WriteAllBytes(localPath, uploadFileBytes); Result.Url = savePath; Result.State = UploadState.Success; } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } return Result; }