Before we start, you need to know how a form uploads files by post, and you're going to add a special enctype attribute to the Form tab, and for that, we need to create a form tag like this:
<% using (Html.BeginForm ("Edit", "person", FormMethod.Post, new {enctype = "multipart/form-data"}) {%>
Then we just need to add an input of type "file", a form of sumbit button. You must make sure that the input has "name" attribute. We can also any one we want on the form:
<table>
<tr>
<td><input type= "file" id= "Picture" name= "Picture"/></td>
</tr>
<tr>
<td><input type= "Submit" value= "Upload"/></td>
</tr>
</table>
Now we have our own form, ready for the server-side action. We do this:
[Acceptverbs (Httpverbs.post)]
Public ActionResult Edit (httppostedfilebase picture)
{
If [picture!= null]
{picture
. SaveAs ("C:\wherever\" + picture. FileName);
}
It's beautiful! Now it's all left to test it. I used MOQ 3 and we just need to mock out a file and put it in the method:
var personpicture = new mock
Wow All can mock out all we have, pretend a little property in the controller, call the method, and then verify that the appropriate method on the picture is invoked. It's so simple!
Hopefully you already know this, but if you don't know, hopefully this post will help you.