TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Upload file in ASP.NET MVC
Mukesh Kumar
Oct 01
2015
Code
1.5
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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