public ActionResult Image(string filename) { var stream = new MemoryStream(); string Thumbnail = ConfigurationManager.AppSettings["Image"]; switch (Thumbnail) { case "Mongo": MongoStorage.GetFile(stream, filename, "Image"); break; case "FileSystem": stream = FileSystemStorage.GetFile(filename, "Image"); if (stream == null) { return(File(new byte[] { }, "image/jpeg")); } break; case "QiNue": //实际上这里直接使用QiNue的URL就可以了 stream = QiniuStorage.GetFile(filename); break; } if (stream == null) { return(null); } return(File(stream.ToArray(), "image/jpeg")); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/json"; string ownerId = context.Request["OwnerId"]; string ArticleId = context.Request["ArticleId"]; var httpfile = context.Request.Files[0]; if (UploadFile.IsAboveLimit(httpfile.ContentLength, ownerId)) { var AboveLimit = new { success = ConstHelper.Fail, message = "上传失败,上传资源超过最大值限制!!" }; context.Response.Write(JsonConvert.SerializeObject(AboveLimit)); return; } UploadFile upload = new UploadFile(); upload.Size = httpfile.ContentLength; Image orgImage = Image.FromStream(httpfile.InputStream); //保存原图到七牛 httpfile.InputStream.Position = 0; string strImage = ConfigurationManager.AppSettings["Image"]; var Savefilename = string.Empty; switch (strImage) { case "QiNue": Savefilename = QiniuStorage.upload(httpfile, ownerId); break; case "Mongo": Savefilename = MongoStorage.InsertFile(httpfile, ownerId, "Image"); break; case "FileSystem": Savefilename = FileSystemStorage.PutFile(httpfile, ownerId, "Image"); break; } string ThumbnailUrl = "/FileSystem/Thumbnail?filename=" + Savefilename; string Thumbnail = ConfigurationManager.AppSettings["Thumbnail"]; if (httpfile.FileName.ToLower().EndsWith(".gif")) { //直接保存 switch (Thumbnail) { case "Mongo": MongoStorage.InsertFile(httpfile, ownerId, "Thumbnail"); break; case "FileSystem": Savefilename = FileSystemStorage.PutFile(httpfile, ownerId, "Thumbnail"); break; } upload.SmallFileSize = upload.Size; } else { //这里是拉伸图片,将图片改为BMP(PDF和画面折衷后的决定) var BitmapImage = ImageHelper.ResizeImage(System.Math.Min(800, orgImage.Width), orgImage.Height, ImageHelper.ResizeMode.W, orgImage); //string Smallfilename = MongoStorage.InsertFile(httpfile, ownerId, "Bussiness"); //返回的名字以DefaultOwnerId开始 var SmallStream = StreamConvert.BytesToStream(StreamConvert.BitmapToBytes(BitmapImage, ImageFormat.Gif)); //保存有时间差,所以使用上一步保存时候的文件名称 //同时Stream关闭后无法获得长度,长度必须先写 upload.SmallFileSize = (int)SmallStream.Length; switch (Thumbnail) { case "Mongo": MongoStorage.InsertStreamWithFixFileName(SmallStream, Savefilename, "Thumbnail"); break; case "FileSystem": FileSystemStorage.InsertStreamWithFixFileName(SmallStream, Savefilename, "Thumbnail"); break; } } upload.Name = Savefilename; upload.ArticleID = ArticleId; UploadFile.InsertUploadFile(upload, ownerId); string strfree = UploadFile.GetFreeVolumnByAccountId(ownerId); var result = new { success = ConstHelper.Success, message = "上传成功,剩余图片空间:" + strfree, url = ThumbnailUrl }; string json = JsonConvert.SerializeObject(result); context.Response.Write(json); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //数据库准备 MongoDbRepository.Init(new[] { "Bussiness" }, "Bussiness"); //本地缩略图保存 string Image = ConfigurationManager.AppSettings["Image"]; string Thumbnail = ConfigurationManager.AppSettings["Thumbnail"]; if (Image == "Mongo" || Thumbnail == "Mongo") { MongoStorage.Init("28031"); } if (Image == "FileSystem" || Thumbnail == "FileSystem") { FileSystemStorage.Init(Server.MapPath("/FileStorage/"), new string[] { "Image", "Thumbnail" }); } //日志 MongoDbRepositoryLog.Init(); //七牛存储 QiniuStorage.Init(ConfigurationManager.AppSettings["QINIU:AK"], ConfigurationManager.AppSettings["QINIU:SK"], ConfigurationManager.AppSettings["QINIU:BUCKET"], ConfigurationManager.AppSettings["QINIU:URLBASE"]); //GitHub GithubAccount.AppName = ConfigurationManager.AppSettings["GITHUB:AppName"]; GithubAccount.ClientID = ConfigurationManager.AppSettings["GITHUB:ClientID"]; GithubAccount.ClientSecret = ConfigurationManager.AppSettings["GITHUB:ClientSecret"]; //QQ QQAccount.appID = ConfigurationManager.AppSettings["QQ:AppID"]; QQAccount.appKey = ConfigurationManager.AppSettings["QQ:AppKey"]; QQAccount.callback = ConfigurationManager.AppSettings["QQ:CallBack"]; QQAccount.authorizeURL = ConfigurationManager.AppSettings["QQ:AuthorizeURL"]; //设置缓存 SetCache(); //设置索引 SetIndex(); string SearchMethod = ConfigurationManager.AppSettings["SearchMethod"]; switch (SearchMethod) { case ConstHelper.MongoTextSearch: //设置Text索引用以检索(ElasticSearch) MongoDbRepository.SetTextIndex(Article.CollectionName, nameof(Article.Title)); MongoDbRepository.SetTextIndex(ArticleContent.CollectionName, nameof(ArticleContent.Content)); MongoDbRepository.SetTextIndex(Comment.CollectionName, nameof(Comment.ContentMD)); break; case ConstHelper.ElasticSearch: //ElasticSearch NEST的初始化 SearchManager.Init(); break; } //加载标签库 var filename = Server.MapPath("/Content/Tag.xlsm"); AdminController.TagFilename = filename; if (File.Exists(filename)) { AdminController.InsertExcelTagInfo(new FileStream(filename, FileMode.Open)); } TagUtility.Init(); //PDF设定 if (ConfigurationManager.AppSettings["DEBUGMODE"] == "true") { FileSystemController.BaseUrl = "http://localhost:60907"; } else { FileSystemController.BaseUrl = ConfigurationManager.AppSettings["URLBASE"]; } FileSystemController.PDFFolder = Server.MapPath("/FileStorage/PDF/"); if (!Directory.Exists(FileSystemController.PDFFolder)) { Directory.CreateDirectory(FileSystemController.PDFFolder); } //Jianshu FileSystemController.JianshuFolder = Server.MapPath("/FileStorage/Jianshu/"); if (!Directory.Exists(FileSystemController.JianshuFolder)) { Directory.CreateDirectory(FileSystemController.JianshuFolder); } //业务配置加载 GetConfig(); //新建临时文件夹 var tempPath = Server.MapPath("/") + "/Temp/"; if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } }