- Asp.net Mvc 5 Download File From Server
- Asp Net Mvc 5 Download File Size
- Asp Net Mvc 5 Tutorial
- Asp Net Mvc Tutorial Pdf
In the above code, I have created POST 'Index(.)' method which will receive uploaded CSV file input from the end-user, then save the CSV file on the web server, then import the stored CSV file using CSVLibraryAK library, then process the import file if applicable, then save & export the resultant CSV file on the web server using CSVLibraryAK library, and finally, display the result of CSV. We will verify the file uploaded, by opening the folder in the Application’s directory. Summary Hence, we have just learned how to upload the file in ASP.NET MVC. I hope this post is useful to developers.
Active1 month ago
I've had it suggested to me that I should use FileResult to allow users to download files from my Asp.Net MVC application. But the only examples of this I can find always has to do with image files (specifying content type image/jpeg).
But what if I can't know the file type? I want users to be able to download pretty much any file from the filearea of my site.
I had read one method of doing this (see a previous post for the code), that actually works fine, except for one thing: the name of the file that comes up in the Save As dialog is concatenated from the file path with underscores (folder_folder_file.ext). Also, it seems people think I should return a FileResult instead of using this custom class that I had found BinaryContentResult.
Find active directory bind dns. Anyone know the 'correct' way of doing such a download in MVC?
EDIT:I got the answer (below), but just thought I should post the full working code if someone else is interested:
Community♦
AndersAnders5,6612121 gold badges9292 silver badges143143 bronze badges
9 Answers
You can just specify the generic octet-stream MIME type:
Tieson T.18.4k33 gold badges5959 silver badges8080 bronze badges
![Asp Asp](/uploads/1/3/3/9/133911580/605896154.jpg)
19.7k44 gold badges4343 silver badges5858 bronze badges
The MVC framework supports this natively. The System.Web.MVC.Controller.File controller provides methods to return a file by name/stream/array.
For example using a virtual path to the file you could do the following.
JonathanJonathan1,19011 gold badge77 silver badges1212 bronze badges
If you're using .NET Framework 4.5 then you use use the MimeMapping.GetMimeMapping(string FileName) to get the MIME-Type for your file. This is how I've used it in my action.
user1023602
Salman Hasrat KhanSalman Hasrat Khan1,28711 gold badge1515 silver badges2323 bronze badges
Open dmg file on virtualbox. Phil Haack has a nice article where he created a Custome File Download Action Result class. You only need to specify the virtual path of the file and the name to be saved as.
I used it once and here's my code.
In my example i was storing the physical path of the files so i used this helper method -that i found somewhere i can't remember- to convert it to a virtual path
Canon mp navigator ex 3.0 download for mac. Here's the full class as taken from Phill Haack's article
Manaf Abu.RousManaf Abu.Rous
Thanks to Ian Henry!
In case if you need to get file from MS SQL Server here is the solution.
Where AppModel is
DeveloperDeveloperEntityFramework
model and MyFiles presents table in your database.FileData is varbinary(MAX)
in MyFiles table.Asp.net Mvc 5 Download File From Server
18.7k6262 gold badges241241 silver badges411411 bronze badges
its simple just give your physical path in directoryPath with file name
benka4,4001111 gold badges4242 silver badges5858 bronze badges
DARSHAN SHINDEDARSHAN SHINDE
hossein zakizadehhossein zakizadeh
if (string.IsNullOrWhiteSpace(fileName)) return Content('filename not present');
Caio AugustoCaio Augusto
Download match app for mac. GetFile should be closing the file (or opening it within a using). Then you can delete the file after conversion to bytes-- the download will be done on that byte buffer.
So in your download method..
CDichterCDichter
Not the answer you're looking for? Browse other questions tagged c#asp.net-mvc-2 or ask your own question.
Active8 months ago
I have created a text file in a folder and zipped that folder and saved @same location for test purpose. I wanted to download that zip file directly on user machine after it is created. I am using dotnetzip library and have done following:
Can someone please suggest how the zip file can be downloaded at user's end.?
ruffin10.3k55 gold badges5656 silver badges103103 bronze badges
user2801336user2801336
5 Answers
You may use the controller's
File
method to return a file, like:If the zip file is not required otherwise to be stored, it is unnecessary to write it into a file on the server:
Milind Anantwar71.4k1919 gold badges7575 silver badges103103 bronze badges
mcserepmcserep
First of all, consider a way without creating any files on the server's disk. Bad practise. I'd recommend creating a file and zipping it in memory instead. Hope, you'll find my example below useful.
Asp Net Mvc 5 Download File Size
Notes to the code above:
- Passing a
MemoryStream
instance requires checks that it's open, valid and etc. I omitted them. I'd rather passed a byte array of the file content instead of aMemoryStream
instance to make the code more robust, but it'd be too much for this example. - It doesn't show how to create a required context (your file) in memory. I'd refer to MemoryStream class for instructions.
5,03122 gold badges3737 silver badges5858 bronze badges
just a fix to Klaus solution: (as I can not add comment I have to add another answer!)
The solution is great but for me it gave corrupted zip file and I realized that it is because of return is before finalizing zip object so it did not close zip and result in a corrupted zip.
so to fix we need to just move return line after using zip block so it works.the final result is :
HamedHHamedH1,34111 gold badge1111 silver badges2323 bronze badges
Asp Net Mvc 5 Tutorial
For those just wanting to return an existing Zip file from the App_Data folder (just dump in your zip files there), in the Home controller create this action method:
Get File is an extention method:
Home controller Index view looks like this:
Asp Net Mvc Tutorial Pdf
The main index file action method:
Where GetFileInformation is an extension method:
John PetersJohn Peters4,12422 gold badges3030 silver badges5252 bronze badges
Create a
Karl AndersonKarl AndersonGET
-only controller action that returns a FileResult
, like this:30.8k1111 gold badges5454 silver badges7272 bronze badges