Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.Net MVC Upload Files to Folder or Server

Aug 1, 2016
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.


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

create new asp.net mvc application to upload files to server
Once click OK new popup will open in that select MVC template and click OK like as shown below

select mvc template to create upload files in asp.net mvc
Once we finished creating application now we will add new folder “uploads” like as shown below

create new folder in asp.net mvc application to upload files
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

Adding new controller to asp.net mvc application
Once we click on Controller new popup will open in that select MVC 5 Controller – Empty and click Add like as shown below

select empty mvc controller to add it in asp.net mvc application
Once click on Add new window will open in that enter name of controller and click Add like as shown below

Give name to controller in asp.net mvc application
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

Add new view in asp.net mvc application
Now give name “uploadfiles” to view and select empty template then click on Add button like as shown below

Give name to view in asp.net mvc application
This newly created view will be added under Views folder like as shown below

Add new view in views folder in asp.net mvc application
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" })) {
<label for="file">Upload File:</label>
<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

Asp.net mvc upload files to folder  or server example result

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

srinivas bolisetti said...

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.

Ajay Sharma said...

hi..can you plze show me how post data like image url and text box data in database table in mvc5

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.