So I've got an MVC 3 application that has a couple places where a text file gets generated and returned in an action using:
return File(System.Text.Encoding.UTF8.GetBytes(someString),
"text/plain", "Filename.extension");
and this works fabulously. Now i've got a situation where I'm trying to return a pair of files in a similar fashion. On the view, i have an action link like "Click here to get those 2 files" and i'd like both files to be downloaded much like the single file is downloaded in the above code snippet.
How can I achieve this? Been searching around quite a bit and haven't even seen this question posed anywhere...
I would advice to create a
zip
file to include both the files using steps(ALGORITHM):Java Syntax (Just for understanding)
Once
zip
file is created, return the newly createdzip
file to download..DOT Net Equivalent using DotNetZip
Hope this helps!
Building on Yogendra Singh's idea and using DotNetZip:
Sorry for bumping an old question but...
Another alternative would be to initiate multiple file downloads using JavaScript, and serve files in two different Action Methods on ASP.NET's side.
You're saying you have a link:
So make this link like this:
I'm using
download.js
script found here but you can find plenty of different other options, see this SO question: starting file download with JavaScript for exampleLook at this SO solution: MVC Streaming Zip File
The advantage of this solution is that it streams the file to the client.
I just implemented this solution a couple of days ago and it worked fantastic.