If I write like this:
form action="Images" method="post" enctype="multipart/form-data"
it works.
But in Razor with '@' it doesn't work. Did I make any mistakes?
@using (Html.BeginForm("Upload", "Upload", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
Select a file <input type="file" name="file" />
<input type="submit" value="Upload" />
</fieldset>
}
My controller looks like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload()
{
foreach (string file in Request.Files)
{
var uploadedFile = Request.Files[file];
uploadedFile.SaveAs(Server.MapPath("~/content/pics") +
Path.GetFileName(uploadedFile.FileName));
}
return RedirectToAction ("Upload");
}
The following code works fine:
and generates as expected:
On the other hand if you are writing this code inside the context of other server side construct such as an
if
orforeach
you should remove the@
before theusing
. For example:As far as your server side code is concerned, here's how to proceed: