.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

File Upload in ASP.Net MVC application

Posted By:Dhananjay Kumar       Posted Date: December 11, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

File Upload in ASP.Net MVC application

Objective:

This article will show how to upload a file from client to server location in ASP.Net MVC application in 5 easy steps.

Step 1:

Create an ASP.Net MVC application.

File->New->Project->web->ASP.Net MVC Application

Step 2:

Creating controller

a. Right click on Controller folder and add a new controller.

image1.gif


FileUpload is name of the controller here. Don't check the checkbox.

Description of HttpPostedFileBase class

For uploading the file in specified path at server HttpPostedFileBase class from System.Web namespace would be used.

HttpPostedFileBase class

  • This contains 4 properties
  • This contains one virtual method.
  • FileName property will be used to fetch the file name of the file.
  • SaveAs method will be used to save the file at server location.


    image2.gif

    b. Type the below code in controller. This code will save the file at server. Location of the saved file would be E:\Temp directory.

    FileUploadController.cs

    image3.gif

    Step 3:

    Creating view
    1. Right click on Edit action and select Add view


      image4.gif
       

    2. Leave the default setting

      image5.gif
       
    3. In Edit.aspx, inside a content place holder, include the following Html tag. This is including control for file to upload.

      image6.gif

      If you see the action parameter for form, it is URL for the action which is actually uploading the file at server.
       
    4. Entire Edit.aspx will look like
    Edit.aspx

    image7.gif

    Step 4:

    Adding link at the main page for file uploading
    1. Open site.master in Views->Shared Folder
    2. Add below line in menu tag 

      image8.gif
       
    3. So the menu element will look like,

      image9.gif
    Step 5:

    Press F5 to run with debug

    image10.gif

    Click on File Upload

    image11.gif


    Conclusion:

    This article showed how to upload a file from client to server location in ASP.Net MVC application.
  •  Subscribe to Articles

         

    Further Readings:

    Responses

    No response found. Be the first to respond this post

    Post Comment

    You must Sign In To post reply
    Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend