/// <summary> /// 添加图片 /// </summary> /// <param name="tiMuImages">The ti mu images.</param> /// <exception cref="System.ArgumentNullException"></exception> public void InsertImage(TiMuImages tiMuImages) { if (tiMuImages == null) { throw new ArgumentNullException(nameof(tiMuImages)); } _tiMuImagesRepository.Insert(tiMuImages); }
/// <summary> /// Uploads the image. /// </summary> /// <returns></returns> public ActionResult UploadImage(HttpPostedFileBase file, Guid tmid, string subjcode) { var timu = _tiMuService.GeTiMuById(tmid); var info = new ImageResult(); if (file != null && file.ContentLength > 0) { string fix = Path.GetExtension(file.FileName)?.ToLower(); if (fix != ".bmp" && fix != ".gif" && fix != ".jpg" && fix != ".png") { info.Error = "请上传bmp、gif、jpg、png格式的文件。"; } else { var tmImageName = Guid.NewGuid(); string reName = tmImageName + fix; //根据科目创建文件夹 string path = "/Content/Upload/" + subjcode + "/"; if (!Directory.Exists(Server.MapPath(path))) { Directory.CreateDirectory(Server.MapPath(path)); } try { using (var img = System.Drawing.Image.FromStream(file.InputStream)) { var tiMuImages = new TiMuImages { Tmid = tmid.ToString(), Status = false, Name = reName, ImgPath = path + reName, UploadDate = DateTime.Now, CustomerId = 0//_workContext.CurrentCustomer.Id }; _tiMuService.InsertImage(tiMuImages); info.ImagePath = "[img=" + img.Width + "," + img.Height + "]" + reName + "[/img]"; file.SaveAs(Server.MapPath(path + reName)); } } catch (Exception e) { info.Error = e.Message; } } } return(View(info)); }