1
如何上傳圖像以進入ASP.NET MVC 3.0中的~/Content/Images
?如何上傳ASP.NET MVC中的圖像?
如何上傳圖像以進入ASP.NET MVC 3.0中的~/Content/Images
?如何上傳ASP.NET MVC中的圖像?
如果你想在ASP.NET MVC中上傳圖片,請嘗試以下問題:
如果你想使用ASP.NET控件上傳圖像,這打破了MVC關注的分離。有upload helpers available。
HTML文件上傳ASP MVC 3
型號:(注意FileExtensionsAttribute是MvcFutures可用它會驗證文件擴展名的客戶端和服務器端。)
public class ViewModel
{
[Required, Microsoft.Web.Mvc.FileExtensions(Extensions = "csv", ErrorMessage = "Specify a CSV file. (Comma-separated values)")]
public HttpPostedFileBase File { get; set; }
}
HTML查看:
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(m => m.File, new { type = "file" })
@Html.ValidationMessageFor(m => m.File)
}
控制器動作:
[HttpPost]
public ActionResult Action(ViewModel model)
{
if (ModelState.IsValid)
{
// Use your file here
using (MemoryStream memoryStream = new MemoryStream())
{
model.File.InputStream.CopyTo(memoryStream);
}
}
}
[上傳使用ASP.NET MVC的圖像(的可能重複http://stackoverflow.com/questions/1379558/uploading-an-image-using-asp-淨MVC) – 2011-01-30 15:28:53