Is there a HTMLHelper
for file upload? Specifically, I am looking for a replace of
<input type="file"/>
using ASP.NET MVC HTMLHelper.
Or, If I use
using (Html.BeginForm())
What is the HTML control for the file upload?
The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!
An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input> and <img> tags.
HTML Upload File ASP MVC 3.
Model: (Note that FileExtensionsAttribute is available in MvcFutures. It will validate file extensions client side and server side.)
public class ViewModel { [Required, Microsoft.Web.Mvc.FileExtensions(Extensions = "csv", ErrorMessage = "Specify a CSV file. (Comma-separated values)")] public HttpPostedFileBase File { get; set; } }
HTML View:
@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) }
Controller action:
[HttpPost] public ActionResult Action(ViewModel model) { if (ModelState.IsValid) { // Use your file here using (MemoryStream memoryStream = new MemoryStream()) { model.File.InputStream.CopyTo(memoryStream); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With