Luis Ortiz
Luis Ortiz

Reputation: 19

How to download 2 or more files

So I need to download 2 files on a MVC Core application

I already have a download file code buy I dont really know how to make it to download 2 or more files

[HttpGet("DownloadSDES")]
        public IActionResult DownloadSDESCypher()
        {
            var net = new System.Net.WebClient();
            var data = net.DownloadData(sDESRepository.ObtainPathDownload());
            var content = new System.IO.MemoryStream(data);
            var contentType = "APPLICATION/octet-stream";
            var fileName = "CypherSDES.scif";
            return File(content, contentType, fileName);

        }

It does download a file but I don't know how to make it to work with 2 or more files

Upvotes: 1

Views: 972

Answers (3)

tmaj
tmaj

Reputation: 34987

Make it a single file; this will be consistent with the way the web works (think downloading files from Internet using your browser).

  1. Download two files on your action.
  2. Zip (or tar) them.
  3. Return the zip.

There is even a snippet for this ready at Using ASP.NET how can I create a zip file from an array of strings?.

Upvotes: 2

Christopher
Christopher

Reputation: 9804

Your question seems wrong. It seems to be "how do I offer two files for download"?

The Obvious Solution is to provide 2 download links. However if it absolutely has to be done in a single file and/or should contain folder structures, .zip files are the droid you are looking for.

If you want to download all code of a project from GitHub, they make a .ZIP file.

If you want to download a entire folder from a HFS instance, it will make you a .ZIP file.

ZipFiles can be easily created (and read/manipulated( using the ZipArchive class. You could do this on the fly, or have the .zip file pre-created in the Database with the source files.

Other formats like .tar, .rar or .7zip can be used. But .zip is the most universally compatible format. Even Windows has native support for it since XP.

Upvotes: 0

Noobie
Noobie

Reputation: 51

If you have a list of urls e.g:

endpoint/file1.pdf, endpoint/file2.pdf, endpoint/file3.pdf ...

You can run your DownloadData method inside a for loop and fetch these files one by one.

You can also use HttpClient since it's a more modern way of making requests asynchronously. See: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.7.2

Upvotes: 1

Related Questions