2011-01-30 94 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); 
     } 
    } 
}