<input type ="file"/>的Html帮助器

Gra*_*ton 123 asp.net-mvc upload html-helper file-upload razor

HTMLHelper文件上传吗?具体来说,我正在寻找替代

<input type="file"/>
Run Code Online (Sandbox Code Playgroud)

使用ASP.NET MVC HTMLHelper.

或者,如果我使用

using (Html.BeginForm()) 
Run Code Online (Sandbox Code Playgroud)

什么是文件上传的HTML控件?

Pau*_*nis 203

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; }
}
Run Code Online (Sandbox Code Playgroud)

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)
}
Run Code Online (Sandbox Code Playgroud)

控制器动作:

[HttpPost]
public ActionResult Action(ViewModel model)
{
    if (ModelState.IsValid)
    {
        // Use your file here
        using (MemoryStream memoryStream = new MemoryStream())
        {
            model.File.InputStream.CopyTo(memoryStream);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


bal*_*dre 19

您还可以使用:

@using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
    <p>
        <input type="file" id="fileUpload" name="fileUpload" size="23" />
    </p>
    <p>
        <input type="submit" value="Upload file" /></p> 
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*son 8

我有一段时间没有同样的问题,遇到了Scott Hanselman的一个帖子:

使用ASP.NET MVC实现HTTP文件上载,包括测试和模拟

希望这可以帮助.


Tod*_*Tod 5

或者你可以正确地做到:

在你的HtmlHelper扩展类中:

public static MvcHtmlString FileFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
    {
        return helper.FileFor(expression, null);
    }

public static MvcHtmlString FileFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
    {
        var builder = new TagBuilder("input");

        var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
        builder.GenerateId(id);
        builder.MergeAttribute("name", id);
        builder.MergeAttribute("type", "file");

        builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

        // Render tag
        return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
    }
Run Code Online (Sandbox Code Playgroud)

这一行:

var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
Run Code Online (Sandbox Code Playgroud)

生成模型独有的ID,您可以在列表和内容中知道.model [0] .Name等

在模型中创建正确的属性:

public HttpPostedFileBase NewFile { get; set; }
Run Code Online (Sandbox Code Playgroud)

然后你需要确保你的表单将发送文件:

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
Run Code Online (Sandbox Code Playgroud)

那么这是你的助手:

@Html.FileFor(x => x.NewFile)
Run Code Online (Sandbox Code Playgroud)