2011-01-30 92 views
1

如何上傳圖像以進入ASP.NET MVC 3.0中的~/Content/Images如何上傳ASP.NET MVC中的圖像?

+1

[上傳使用ASP.NET MVC的圖像(的可能重複http://stackoverflow.com/questions/1379558/uploading-an-image-using-asp-淨MVC) – 2011-01-30 15:28:53

回答

1

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); 
     } 
    } 
}