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
File Download Sample in MVC: Day 40
WhatsApp
Akshay Patel
10y
114.2k
0
7
100
Article
Let's see the step-by-step implementation:
Create an MVC project from the "Empty" template.
Right-click on "Controllers" and select "Add" >> "Controller...".
Select "MVC 5 Controller - Empty" to add an empty controller.
Click on the "Add" button.
Name the controller "HomeController".
The Index() action result method will be added.
Create an Images folder and add some images inside that for the download.
Create an action method, "Download".
To add a view, right-click on "Download" and select "Add View...".
Name the view and select "Empty (without model)" as the template. Click on the "Add" button.
In Download.cshtml we get the list of images in the Model. We create an individual action link by iterating the model for all the images. In the actionlink we specify "DownloadFile" as the action method.
Here we create two action methods. One is to get the list of images and another one is to download the file. In the Download action result we get the list of files using DirectoryInfo and FileInfo. The DownloadFile action result returns the FilePathResult with filename and content type.
Now run the project and you can see all the images with download link.
Click on any of the images and you have an option to save the image. Press the "OK" button to save the image.
<<
Multiple File Upload Sample Using HttpFileCollectionBase in MVC: Day 39
ASP.NET
File Download in MVC
MVC
Up Next
Ebook Download
View all
Diving Into ASP.NET WebAPI
Read by 21.5k people
Download Now!
Learn
View all
Membership not found