Introduction:
Here I will explain how to upload files to server in asp.net mvc 5 with example or asp.net mvc upload files to folder with example or uploading files with asp.net mvc or how to upload files to directory in asp.net mvc with example or handle file upload in asp.net mvc with example. In asp.net mvc we can upload files by using HttpPostedFileBase property.
Description:
In previous articles I explained asp.net mvc global action filters with example, for loop example in asp.net mvc razor view, razor view engine in asp.net mvc with example, asynchronous controllers in asp.net mvc with example, difference between viewbag, viewdata and tempdata with example, validation using fluent validation in asp.net mvc and many articles relating to asp.net mvc, asp.net, c#,vb.net. Now I will explain how to upload files to server or folder in asp.net mvc with example.
In previous articles I explained asp.net mvc global action filters with example, for loop example in asp.net mvc razor view, razor view engine in asp.net mvc with example, asynchronous controllers in asp.net mvc with example, difference between viewbag, viewdata and tempdata with example, validation using fluent validation in asp.net mvc and many articles relating to asp.net mvc, asp.net, c#,vb.net. Now I will explain how to upload files to server or folder in asp.net mvc with example.
To
upload files to server / folder in asp.net mvc first we need to
create application for that Open visual studio --> Go to File -->
Select New --> Project like as
shown below
Once
we select Project new popup will open in that select Asp.Net Web Application
and give name to application and click OK like as shown below
Once
click OK new popup will open in that select MVC template and click OK like as
shown below
Once
we finished creating application now we will add new folder “uploads”
like as shown below
Now
we will add new controller to upload files in folder for that right click on
Controller folder --> select Add --> Controller like as shown below
Once
we click on Controller new popup will open in that select MVC 5 Controller –
Empty and click Add like as shown below
Once
click on Add new window will open in that enter name of controller and click
Add like as shown below
Now
open controller and write the code like as shown below
[HttpGet]
public ActionResult
UploadFile()
{
return View();
}
[HttpPost]
public ActionResult
UploadFile(HttpPostedFileBase file)
{
try
{
if (file!=null && file.ContentLength > 0)
{
string filename = Path.GetFileName(file.FileName);
string path = Path.Combine(Server.MapPath("~/uploads"), filename);
file.SaveAs(path);
ViewBag.Result = "File Uploaded Successfully";
}
else {
ViewBag.Result = "No File Uploaded";
}
}
catch
{
ViewBag.Result = "File upload failed";
}
return View();
}
|
Now we will add view to our
controller action method for that right click on uploadfiles action method --> select Add View like as shown
below
Now give name “uploadfiles” to view and select empty
template then click on Add button
like as shown below
This newly created view will be added under Views folder like as shown below
Now open that view and
write the code like as shown below
@{
ViewBag.Title = "UploadFile";
}
<h2>Upload Files</h2>
@using (Html.BeginForm("UploadFile","Upload",FormMethod.Post, new { enctype = "multipart/form-data" })) {
<input type="file" name="file" id="file" /><br />
<input type="submit" value="Upload
File" /><br /><br />
@ViewBag.Result
}
|
Now run the application (url
always in the format of http://localhost:portnumber/controller
name/action method name) and check the output that would be like as shown
below
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
5 comments :
Could you please post how to get pagination for a listview in Asp.net MVC without using entity framework and with mysql db in server.
hi..can you plze show me how post data like image url and text box data in database table in mvc5
sir how can i use this in vs 2012 help me out this
How can use in vs2010
How can save the filepath in database(sql server)
Note: Only a member of this blog may post a comment.