我正在使用 MVC3 通过网络表单发送图像集合。控制器接收此发布的图像并将其名称保存到数据库中。
[HttpPost]
public ActionResult Edit(MyViewModel data, IEnumerable<HttpPostedFileBase> postedImages)
{
if (ModelState.IsValid)
{
using (session...and transaction...)
{
MyModel model = session.Get<MyModel>(data.Id);
data.SendToDomainModel(model, session);
foreach (var image in postedImages)
{
if ((image != null) && (image.ContentLength > 0))
{
Photo photo = new Photo();
var fileName = Path.GetFileName(image.FileName);
// path used to save actuall image to the hdd path
var pathToSave = Path.Combine(Server.MapPath("~/Content/uploads"), fileName);
// path used to save image path inside db column
var path = Path.Combine("/Content/uploads/" + fileName);
photo.MyModel= session.Load<MyModel>(model.Id);
photo.Path = path;
photo.Name = fileName;
image.SaveAs(pathToSave);
model.Photos.Add(photo);
}
}
// commit transaction ..
// save session ..
}
return RedirectToAction("Index");
}
else { return View(data); }
}
如何使用图像集合中的第一张图像并使用文件名前缀“firstImage”复制它并裁剪为 50x50px 尺寸?
谢谢