I build a asp.net mvc 5 project and some point i want to download file, the file can be anything for example: 'pdf', 'doc', 'zip'.
I try the following code:
[HttpGet]
[Route("Download")]
public ActionResult DownloadFiles(string data)// data is the name folder where the file/files exists
{
string pathFolder = $@"{db.GetDetails().PathToFolder}\{data}";//PathToFolder is the root folder
if (Directory.Exists(pathFolder))
{
string[] files = Directory.GetFiles(pathFolder);
if (files.Length > 1)
{
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(pathFolder);
MemoryStream output = new MemoryStream();
zip.Save(output);
return File(output, "application/zip", "zipfile.zip");
}
}
else if (files.Length == 1)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(files[0]);
string fileName = Path.GetFileName(files[0]);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
}
return null;
}
js code:
$.ajax({
type: 'GET',
url: '/controllerforexample/Download',
data: { data: data }// the first date is the variable in the action and the second data is a text.
});
But nothing happens.
Someone maybe know what can be the problem?
Thanks,
Tal