File Upload ASP.NET MVC 3.0

File Upload ASP.NET MVC 3.0

You don't use a file input control. Server side controls are not used in ASP.NET MVC. Checkout the following blog post which illustrates how to achieve this in ASP.NET MVC.

So you would start by creating an HTML form which would contain a file input:

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
}

and then you would have a controller to handle the upload:

public class HomeController : Controller
{
// This action renders the form
public ActionResult Index()
{
return View();
}

// This action handles the form POST and the upload
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the filename
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
}

ASP.NET MVC 3 - File Upload

You should use HttpPostedFileBase for you controller and do something like that

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{

if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/"), fileName);
file.SaveAs(path);
}

return RedirectToAction("Index");
}

And for the view

<form action="" method="post" enctype="multipart/form-data">

<label for="file">Filename:</label>
<input type="file" name="file" id="file" />

<input type="submit" />
</form>

Check Phil Haack blog here for this problem: Uploading a File (Or Files) With ASP.NET MVC

MVC 3 file upload and model binding

Your form doesn't contain any input tag other than the file so in your controller action you cannot expect to get anything else than the uploaded file (that's all that's being sent to the server). One way to achieve this would be to include a hidden tag containing the id of the model which will allow you to retrieve it from your datastore inside the controller action you are posting to (use this if the user is not supposed to modify the model but simply attach a file):

@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.HiddenFor(x => x.Id)
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
}

and then in your controller action:

[HttpPost]
public ActionResult Uploadfile(int id, HttpPostedFileBase file)
{
Containers containers = Repository.GetContainers(id);
...
}

On the other hand if you wanted to allow the user to modify this model then you will need to include the proper input fields for each field of your model that you want to be sent to the server:

@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(x => x.Prop1)
@Html.TextBoxFor(x => x.Prop2)
@Html.TextBoxFor(x => x.Prop3)
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
}

and then you will have the default model binder reconstruct this model from the request:

[HttpPost]
public ActionResult Uploadfile(Container containers, HttpPostedFileBase file)
{
...
}

MVC3, multiple file upload, model binding

Create separate sections in the view for the purchase requisitions and bid results. Something like this:

<form action="" method="post" enctype="multipart/form-data">

<h3>Purchase Requistions</h3>
<label for="file1">Filename:</label>
<input type="file" name="purchasereqs" id="file1" />

<label for="file2">Filename:</label>
<input type="file" name="purchasereqs" id="file2" />

<h3>Bid Results</h3>
<label for="file3">Filename:</label>
<input type="file" name="bidresults" id="file3" />

<label for="file4">Filename:</label>
<input type="file" name="bidresults" id="file4" />

<input type="submit" />
</form>

Then you would have an action signature like this:

[HttpPost]
public ActionResult Create(
TransactionViewModel model,
IEnumerable<HttpPostedFileBase> purchasereqs,
IEnumerable<HttpPostedFileBase> bidresults)
{
//save to database
}


Related Topics



Leave a reply



Submit