/// <summary> /// 上传图片 /// </summary> /// <returns></returns> public async Task<IHttpActionResult> Post() { //如果不含文件就退出 if (!HttpContext.Current.Request.Files.AllKeys.Any()) { return Json(new { Code = 1 }); } var httpPostedFile = HttpContext.Current.Request.Files[0]; if (httpPostedFile == null) { return Json(new { Code = 1 }); } BlobHelper blob = new BlobHelper(BlobString.Portrait); string fileName = Guid.NewGuid().ToString(); blob.Upload( httpPostedFile, fileName); bool isPicture = false; switch(httpPostedFile.ContentType) { case "image/jpeg": case "image/png": isPicture = true; break; default: break; } //插入数据库,如果是图像文件,生成缩略图,大图 DataBaseEntities db = new DataBaseEntities(); pm_Attach attach = new pm_Attach { AddDate = DateTime.Now, AttachId = Guid.NewGuid().ToString(), ContentType = httpPostedFile.ContentType, IsPicture = isPicture, VersionId = HttpContext.Current.Request.Form["versionid"], Url = "http://hdy.awblob.com/portrait/" + fileName, DisplayName = httpPostedFile.FileName }; db.pm_Attach.Add(attach); db.SaveChanges(); if (isPicture) { string fileSaveName = Guid.NewGuid() + ".jpg"; var fileOrginalFile = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload"), fileSaveName); httpPostedFile.SaveAs(fileOrginalFile); string fileResized300Name = HttpContext.Current.Server.MapPath("~/upload/" + Guid.NewGuid().ToString() + ".jpg"); using (var imageFactory = new ImageFactory(preserveExifData: true)) { System.Drawing.Size size = new System.Drawing.Size(300, 300); ResizeLayer resize = new ResizeLayer(size, ResizeMode.Crop); imageFactory.Load(fileOrginalFile).Resize(resize).Save(fileResized300Name); } await blob.UploadFile(fileResized300Name, fileName+"-preview", httpPostedFile.ContentType); string fileResizedBigName = HttpContext.Current.Server.MapPath("~/upload/" + Guid.NewGuid().ToString() + ".jpg"); using (var imageFactory = new ImageFactory(preserveExifData: true)) { System.Drawing.Size size = new System.Drawing.Size(800, 1600); ResizeLayer resize = new ResizeLayer(size, ResizeMode.Max); imageFactory.Load(fileOrginalFile).Resize(resize).Save(fileResizedBigName); } await blob.UploadFile(fileResizedBigName, fileName + "-big", httpPostedFile.ContentType); } // Get the uploaded image from the Files collection return Json(new { Code = 10000, Detail = "http://hdy.awblob.com/portrait/" + fileName }); }
public async Task<IHttpActionResult> fileUpload() { //如果不含文件就退出 if (!HttpContext.Current.Request.Files.AllKeys.Any()) { return Json(new { Code = 1 }); } string fileName = Guid.NewGuid().ToString(); var httpPostedFile = HttpContext.Current.Request.Files[0]; if (httpPostedFile == null) { return Json(new { Code = 1 }); } string fileExtension = System.IO.Path.GetExtension(HttpContext.Current.Request.Form["file"]).ToLower(); if (fileExtension == "") { fileExtension = ".jpg"; } var fileOrginalFile = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload"), fileName + fileExtension); // Save the uploaded file to "UploadedFiles" folder httpPostedFile.SaveAs(fileOrginalFile); string fileResized300Name = HttpContext.Current.Server.MapPath("~/upload/" + Guid.NewGuid().ToString() + fileExtension); using (var imageFactory = new ImageFactory(preserveExifData: true)) { System.Drawing.Size size = new System.Drawing.Size(300, 300); ResizeLayer resize = new ResizeLayer(size, ResizeMode.Crop); imageFactory.Load(fileOrginalFile).Resize(resize).Save(fileResized300Name); } BlobHelper blob = new BlobHelper(BlobString.Portrait); string contentType = "application/octet-stream"; switch (fileExtension) { case ".jpg": case ".jpeg": contentType = "image/jpeg"; break; case ".png": contentType = "image/png"; break; default: contentType = "application/octet-stream"; break; } await blob.UploadFile(fileResized300Name, fileName, contentType); // Get the uploaded image from the Files collection return Json(new { Code = 10000, Detail = "http://hdy.awblob.com/portrait/" + fileName }); }