asp.net mvc to achieve simple upload function _ practical skills

Source: Internet
Author: User
Method One:
Code in the Home/index.aspx
Copy Code code as follows:

<% using (Html.BeginForm ("Up", "Home", formmethod.post,new{enctype= "Multipart/form-data"}) {%>
<input type= "File" name= "Upfile"/>
<input type = "Submit" name = "Upload" value = "Upload"/>
<%}%>

Code in the HomeController
[Code]
[Acceptverbs (Httpverbs.post)]
Public ActionResult Up (HttpPostedFileBase upfile)
{
if (upfile!= null)
{
if (upfile. ContentLength > 0)
{
Upfile. SaveAs ("d:\\7.jpg");
}
}
Return redirecttoaction ("Index");
}

Method Two:



Code in the Home/index.aspx
Copy Code code as follows:

<form action= "<%=url.action (" upload2 ")%>" enctype= "Multipart/form-data" method= "Post >
<input name= "up1" type= "file"/><input type= "Submit"/>
</form>

Code in the HomeController
Copy Code code as follows:

Public ActionResult upload2 (HttpPostedFileBase up1)
{
Up1. SaveAs ("d:\\8.jpg");
Return Content (up1. FileName);
}
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.