HTML帮手&LT;输入类型=&QUOT;文件&QUOT; /&GT; [英] Html helper for <input type="file" />
本文介绍了HTML帮手&LT;输入类型=&QUOT;文件&QUOT; /&GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有文件上传一的HtmlHelper?具体来说,我要寻找一个代替
Is there a HTMLHelper for file upload? Specifically, I am looking for a replace of
<input type="file"/>
使用ASP.NET MVC的HtmlHelper。
using ASP.NET MVC HTMLHelper.
或者,如果我使用
using (Html.BeginForm())
什么是文件上传HTML控件?
What is the HTML control for the file upload?
推荐答案
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);
}
}
}
这篇关于HTML帮手&LT;输入类型=&QUOT;文件&QUOT; /&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文