在ASP.NET MVC网站中存储图像有多种解决方法,以下是其中一种常见的方法:
public class ImageUploader
{
public static string UploadImage(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
file.SaveAs(path);
return fileName;
}
return null;
}
}
@using (Html.BeginForm("UploadImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
}
public class HomeController : Controller
{
[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase file)
{
var fileName = ImageUploader.UploadImage(file);
// 处理图像上传后的逻辑
return RedirectToAction("Index");
}
}
通过以上步骤,用户可以在MVC网站中上传图像,并将其存储在指定的位置(在这个例子中是~/Content/Images目录下)。您可以根据自己的需求对代码进行调整和扩展。