Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Upload a file in ASP.NET MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 252
    Comment on it

    The following post captures the implementation details for uploading file in ASP.NET MVC. First we need to create HTML form which would receive the file as input:

     @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
       {
            <in put type="file" name="file" />
            <in put type="submit" value="OK" />
       }
    

    Now, we create action for file upload, below is the code:

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0) 
        {
            var name = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/uploads"), name);
            file.SaveAs(path);
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: