Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html helper for <input type="file" />

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?

like image 409
Graviton Avatar asked Nov 20 '08 08:11

Graviton


People also ask

How do you input a file in HTML?

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!

What is helper in HTML?

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.


1 Answers

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);         }     } } 
like image 128
Paulius Zaliaduonis Avatar answered Sep 23 '22 02:09

Paulius Zaliaduonis