and the other using Razor. For any of my code samples in this blog post I will be using the later.
The first thing we will take a look at is what I think is the source of most of the confusion: the Html.Beginform() method. Let’s dissect this a bit further:
Html.BeginForm("ProcessSubmit", "Upload", FormMethod.Post, new { id = "uploadForm", enctype = "multipart/form-data" })
What kind of HTML does this generate? Placing this in a regular view, loading the page and then viewing the source of the page we see the following:
<
form
action
=
"/Upload/ProcessSubmit"
enctype
=
"multipart/form-data"
id
=
"uploadForm"
method
=
"post"
></
form
>
<
form
action
=
"/Upload/ProcessSubmit"
enctype
=
"multipart/form-data"
id
=
"uploadForm"
method
=
"post"
><
div
class
=
"t-widget t-upload"
><
div
class
=
"t-button t-upload-button"
><
span
>Select...</
span
><
input
id
=
"attachments"
name
=
"attachments"
type
=
"file"
/></
div
></
div
> <
p
class
=
"note"
>
Maximum combined file size: 10 MB
</
p
>
<
div
style
=
"margin: 20px 0 0 0;"
>
<
input
type
=
"submit"
value
=
"Send"
class
=
"t-button"
/>
<
input
type
=
"reset"
value
=
"Reset"
class
=
"t-button"
/>
</
div
>
</
form
>
Now we can take a look at what happens on the controller side of things. ProcessSubmit is the ActionResult we are interested in here, so let’s see what it does:
[HttpPost]
public
ActionResult ProcessSubmit(IEnumerable<HttpPostedFileBase> attachments)
{
if
(attachments !=
null
)
{
TempData[
"UploadedAttachments"
] = GetFileInfo(attachments);
}
return
RedirectToAction(
"SyncUploadResult"
);
}
private
IEnumerable<
string
> GetFileInfo(IEnumerable<HttpPostedFileBase> attachments)
{
return
from a
in
attachments
where a !=
null
select
string
.Format(
"{0} ({1} bytes)"
, Path.GetFileName(a.FileName), a.ContentLength);
}
First we see that it is decorated with the [HttpPost] attribute, which goes along with our FormMethod that we set up. The next thing that sticks out is the parameters that it accepts. Here we see an IEnumerable of the HttpPostedFileBase type which allows us to grab information such as FileName, ContentType and ContentLength from our uploaded files. What might seem familiar to you is the name of the variable being passed (don’t worry if it’s not) which is actually the same as what we named our Upload component. This is actually a necessity in order to get all of the uploaded files into this function. Via this variable we can now get any information that we might need from our files, which is what the GetFileInfo function takes care of. This simply returns a string with the file name and size of our files.
Our demo saves this to TempData and then redirects to another view (SyncUploadResult). This view simply just displays all of the information we just added to TempData, so this isn’t far too interesting. So this means that the function you want to do all of your saving in is “ProcessSubmit” (or in your case, the actionName you specified in the BeginForm method).
That should take care of demystifying what is happening in our Synchronous Upload demo! As a side-effect you also walked away with some more knowledge regarding MVC, how awesome is that?
Carl Bergenhem was the Product Manager for Kendo UI.