MVC - file upload - asp.net-mvc

Hey...
I have upload control on my view. Is there a way to associate this control with model data(something like LabelFor or TextBoxFor). I need this, because on page load I loose my information in file upload control
Thx

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);
}
}
}

Yes, use the HttpPostedFileBase class for the property type and it will bind just like any other property would.

Related

File Upload as Part of Form with Other Fields

I have an ASP.NET MVC website. I need a page where the user must enter several fields, including an image file.
I could find many, many references for uploading a file using MVC. But they don't upload the file as part of a form with other fields.
Ideally, fields and file will be sent to a single controller. Any tips?
If you do not use third party libraries, try this:
Model
public class Strategy
{
public int ID { get; set; }
public string Name { get; set; }
public byte[] File { get; set; }
}
View
#model TEST.Model.Strategy
#using (Html.BeginForm("Add", "Strategy", FormMethod.Post, new { #id = "frmStrategy", enctype = "multipart/form-data" }))
{
#Html.TextBoxFor(x => x.Name)
<input id="templateFile" name="templateFile" type="file" />
#Html.HiddenFor(x => x.ID)
}
Controller
[HttpPost]
public ActionResult Add(Strategy model, HttpPostedFileBase templateFile)
{
if (templateFile != null && templateFile.ContentLength > 0)
{
try
{
var fname = Path.GetFileName(templateFile.FileName);
using (MemoryStream ms = new MemoryStream())
{
templateFile.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
model.File = array;
}
...
You can use FineUploader. See Demo
Valums Uploader. It uses pure Javascript (uploads file using Iframe)
You might need to use a client plugin. Plupload is one possible choice. And here's an example of how you could integrate it in your MVC application. Another popular plugin which supports this functionality is Uploadify.
Asp.net mvc 3 file uploads using the fileapi
See Progress Demo 1, 2 & 3 at http://jquery.malsup.com/form/#file-upload
Ref: http://forums.asp.net/t/1897410.aspx/1?MVC4+File+Upload

Uploading Files into Database with ASP.NET MVC

I want to give a facility on my form for user to upload files and save in Database.
How is this done in ASP.NET MVC.
What DataType to write in my Model Class. I tried with Byte[], but during the scaffolding the solution could not generate the appropriate HTML for it in the corresponding View.
How are these cases handled?
You could use a byte[] on your model and a HttpPostedFileBase on your view model. For example:
public class MyViewModel
{
[Required]
public HttpPostedFileBase File { get; set; }
}
and then:
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
byte[] uploadedFile = new byte[model.File.InputStream.Length];
model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
// now you could pass the byte array to your model and store wherever
// you intended to store it
return Content("Thanks for uploading the file");
}
}
and finally in your view:
#model MyViewModel
#using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
#Html.LabelFor(x => x.File)
#Html.TextBoxFor(x => x.File, new { type = "file" })
#Html.ValidationMessageFor(x => x.File)
</div>
<button type="submit">Upload</button>
}

Inconvenient to upload files to the server and use TempData

I'm trying to make a file upload to the server using Uploadify, but not working the TempData to pass variables between controllers and I have not found an error.
I'm trying passing the variables fileName and file with TempData of the controller "GetFile" to the controller "ModelCreate".
The controller "GetFile" works well, but when I check the value of "date1" and "date2" in the controller "ModelCreate" is null
I just want to make the file saved in the controller "ModelCreate"
public string GetFile(HttpPostedFileBase file)
{
var fileName = this.Server.MapPath("~/Informs/" + System.IO.Path.GetFileName(file.FileName));
if (System.IO.File.Exists(fileName))
return "has been uploaded successfully";
file.SaveAs(fileName);
TempData["NameFile"] = fileName;
TempData["File"] = file;
return "1";
}
[HttpPost]
public ActionResult ModelCreate(INFORME inform)
{
var date1 = TempData["NameFile"] as string;
var date2 = TempData["File"] as HttpPostedFileBase;
date2.SaveAs(date1);
.
.
.
.
}
why "date1" and "date2" are null?
Blessings
There's not enough information to provide an answer to this question. As requested in the comments section I will provide a full example illustrating a form allowing the user to fill a couple of input fields and upload a file.
As always we start by defining the view model which will reflect the information that we want to display on the view:
public class MyViewModel
{
[Required]
public string TextField { get; set; }
[DataType(DataType.MultilineText)]
public string TextAreaField { get; set; }
public bool CheckBoxField { get; set; }
[Required]
public HttpPostedFileBase FileField { get; set; }
}
Then we could have a controller with 2 actions: a GET action that simply displays the form and a POST action that processes the form information when submitted:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
// there were validation errors => redisplay the view
return View(model);
}
// at this stage the model is valid => we could do some processing
// first let's save the file
var appData = Server.MapPath("~/app_data");
var file = Path.Combine(appData, Path.GetFileName(model.FileField.FileName));
model.FileField.SaveAs(file);
// then we could process the other properties
// ...
return Content("Thanks for submitting the data");
}
}
and finally a strongly typed view top the view model:
#model MyViewModel
#Html.ValidationSummary(false)
#using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
#Html.EditorFor(x => x.TextField)
#Html.ValidationMessageFor(x => x.TextField)
</div>
<div>
#Html.EditorFor(x => x.TextAreaField)
#Html.ValidationMessageFor(x => x.TextAreaField)
</div>
<div>
#Html.CheckBoxFor(x => x.CheckBoxField)
#Html.ValidationMessageFor(x => x.CheckBoxField)
</div>
<div>
#Html.LabelFor(x => x.FileField)
#Html.TextBoxFor(x => x.FileField, new { type = "file" })
</div>
<button type="submit">OK</button>
}

How do I upload images in ASP.NET MVC?

How do I upload images to go into ~/Content/Images in ASP.NET MVC 3.0?
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 want to upload images in ASP.NET MVC, try these questions:
Simple Image Upload in ASP.NET MVC
Uploading an image in ASP.NET MVC
If you're wanting to use an ASP.NET control to upload images, that breaks the separation of concerns for MVC. There are upload helpers available.

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?
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);
}
}
}
You can also use:
#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>
}
Or you could do it properly:
In your HtmlHelper Extension class:
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));
}
This line:
var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
Generates an id unique to the model, you know in lists and stuff. model[0].Name etc.
Create the correct property in the model:
public HttpPostedFileBase NewFile { get; set; }
Then you need to make sure your form will send files:
#using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
Then here's your helper:
#Html.FileFor(x => x.NewFile)
I had this same question a while back and came across one of Scott Hanselman's posts:
Implementing HTTP File Upload with ASP.NET MVC including Tests and Mocks
Hope this helps.
Improved version of Paulius Zaliaduonis' answer:
In order to make the validation work properly I had to change the Model to:
public class ViewModel
{
public HttpPostedFileBase File { get; set; }
[Required(ErrorMessage="A header image is required"), FileExtensions(ErrorMessage = "Please upload an image file.")]
public string FileName
{
get
{
if (File != null)
return File.FileName;
else
return String.Empty;
}
}
}
and the view to:
#using (Html.BeginForm("Action", "Controller", FormMethod.Post, new
{ enctype = "multipart/form-data" }))
{
#Html.TextBoxFor(m => m.File, new { type = "file" })
#Html.ValidationMessageFor(m => m.FileName)
}
This is required because what #Serj Sagan wrote about FileExtension attribute working only with strings.
To use BeginForm, here's the way to use it:
using(Html.BeginForm("uploadfiles",
"home", FormMethod.POST, new Dictionary<string, object>(){{"type", "file"}})
This also works:
Model:
public class ViewModel
{
public HttpPostedFileBase File{ get; set; }
}
View:
#using (Html.BeginForm("Action", "Controller", FormMethod.Post, new
{ enctype = "multipart/form-data" }))
{
#Html.TextBoxFor(m => m.File, new { type = "file" })
}
Controller action:
[HttpPost]
public ActionResult Action(ViewModel model)
{
if (ModelState.IsValid)
{
var postedFile = Request.Files["File"];
// now you can get and validate the file type:
var isFileSupported= IsFileSupported(postedFile);
}
}
public bool IsFileSupported(HttpPostedFileBase file)
{
var isSupported = false;
switch (file.ContentType)
{
case ("image/gif"):
isSupported = true;
break;
case ("image/jpeg"):
isSupported = true;
break;
case ("image/png"):
isSupported = true;
break;
case ("audio/mp3"):
isSupported = true;
break;
case ("audio/wav"):
isSupported = true;
break;
}
return isSupported;
}
List of contentTypes
This is a little hacky I guess, but it results in the correct validation attributes etc being applied
#Html.Raw(Html.TextBoxFor(m => m.File).ToHtmlString().Replace("type=\"text\"", "type=\"file\""))

Resources