private BFInfo SaveBF(BLLBFInfo bllbf, HttpPostedFileBase hf, int sysid, int bfid, int width, int height) { string filename = hf.FileName; string extname = System.IO.Path.GetExtension(filename).ToLower(); string fname = System.IO.Path.GetFileNameWithoutExtension(filename); Stream newfile = hf.InputStream; BFInfo bfinfo; if (hf.ContentType.ToLower().Contains("image")) { Image image = System.Drawing.Image.FromStream(hf.InputStream); newfile = GetThumbnail(image, width, height); } newfile.Seek(0, SeekOrigin.Begin); OssClient oc = new OssClient(AccessKeyID, AccessKeySecret); string newfilename = DateTime.Now.ToString("yyMMdd") + "/" + Guid.NewGuid().ToString(); PutObjectResult pr = oc.PutObject(BucketName, newfilename, newfile); if (bfid == 0) { bfinfo = new BFInfo(); bfinfo.SysID = sysid; bfinfo.OrignName = fname; bfinfo.ExtName = extname; bfinfo.MineType = hf.ContentType; bfinfo.LastViewDateTime = bfinfo.CrtDateTime = DateTime.Now; bfinfo.OSSFileName = newfilename; bfinfo.GetCount = 0; bllbf.Add(bfinfo); } else { bfinfo = bllbf.Find(bfid); bfinfo.OrignName = fname; bfinfo.ExtName = extname; bfinfo.MineType = hf.ContentType; bfinfo.CrtDateTime = DateTime.Now; bfinfo.OSSFileName = newfilename; bfinfo.GetCount = 0; bllbf.UpDate(bfinfo); } return bfinfo; }
public ActionResult GetFile(int id, int width = 0, int height = 0) { int bfid = id; BLLBFInfo bllbfinfo = new BLLBFInfo(); var bfinfo = bllbfinfo.Find(bfid); if (bfinfo == null) { return Content("不存在的数据"); } else { bfinfo.LastViewDateTime = DateTime.Now; bfinfo.GetCount++; bllbfinfo.UpDate(bfinfo); //返回一个二进制流!根据类型,来判断,需要返回的内容! MemoryStream ms = new MemoryStream(); OssClient oc = new OssClient(AccessKeyID, AccessKeySecret); GetObjectRequest getObjectRequest = new GetObjectRequest(BucketName, bfinfo.OSSFileName); oc.GetObject(getObjectRequest, ms); if (bfinfo.MineType.Contains("image")) { if (width == 0 && height == 0) { return File(ms.ToArray(), bfinfo.MineType); } else { Image image = System.Drawing.Image.FromStream(ms); return File(GetThumbnail(image, width, height).ToArray(), bfinfo.MineType); } } else { return File(ms.ToArray(), bfinfo.MineType); } } }
public ActionResult Delete(int bfid) { BLLBFInfo bllbfinfo = new BLLBFInfo(); var bfinfo = bllbfinfo.Find(bfid); OssClient oc = new OssClient(AccessKeyID, AccessKeySecret); oc.DeleteObject(BucketName, bfinfo.OSSFileName); bllbfinfo.Delete(bfinfo); return View(); }