I need to upload a file and I have found several ways to do so. The best way I thought to do it was according to this blog: Blog
I also found a usefull post here: stackoverflow topic
But when I try it, it fails. I get the following error in visual studio:
Sequence contains more than one element, with not much more to go further on.
My code looks like this:
Controller:
public PartialViewResult Index(parameterlist)
{
var model = new Model();
return PartialView(model);
}
[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);
var path = Path.Combine(Server.MapPath(@"Path"), fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
View:
<div class="span6">
@using (Html.BeginForm("null", "null", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
}
</div>
So the moment this partialview gets called it goes to the actionResult method, ofcourse there is no file selected because the view hasn't been opened yet. There are some other textboxes and dropdowns in the view. But nothing special, anyone has an idea on what it is that I'm doing wrong? I've got the feeling I'm missing something crucial ...