<input type=“file"的 Html 助手/&gt; [英] Html helper for &lt;input type=&quot;file&quot; /&gt;

查看:23
本文介绍了<input type=“file"的 Html 助手/&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);
        }
    }
}

这篇关于<input type=“file"的 Html 助手/&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆