asp.net MVC 1.0 RTM implementation of File upload

Source: Internet
Author: User
Tags file upload

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.