FileUpload in MVC in ASP.Net using C#

Create view

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVCTEST1.Models.FileUploadModel>" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>FileUpload</title>

</head>

<body>

    <form action="/Test/Upload1" method="post" enctype="multipart/form-data">

    <label>

        Filename:

        <input type="file" name="file" />

    </label>

    <input type="submit" value="Submit" />

    </form>

</body>

</html>

Create controller

#region For FileUpload Page

public ActionResult FileUpload()

{

return View();

}

public ActionResult Upload1(HttpPostedFileBase file)

{

var fileName = Path.GetFileName(file.FileName);

// string path = Server.MapPath("~/Uploads/");

string path = @"D:\test\";

file.SaveAs(path + fileName);

return RedirectToAction("FileUpload");

}

#endregion

And run your page.
 

Ebook Download
View all
Learn
View all