Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Upload file in ASP.NET MVC
WhatsApp
Mukesh Kumar
Oct 01
2015
1.5
k
0
0
Your View
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new {
enctype
=
"multipart/form-data"
}))
{
<
input
type
=
"file"
name
=
"file"
/>
<
input
type
=
"submit"
value
=
"OK"
/>
}
Your controller
ublic
class
HomeController : Controller
{
// This action renders the form
public
ActionResult Index()
{
return
View();
}
// This action handles the form POST and the upload
[HttpPost]
public
ActionResult Index(HttpPostedFileBase file)
{
// Verify that the user selected a file
if
(file !=
null
&& file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath(
"~/App_Data/uploads"
), fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
return
RedirectToAction(
"Index"
);
}
}
ASP.NET
MVCUpload file in ASP.NET MVC
Up Next
Upload file in ASP.NET MVC