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
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:
public FileResult DownLoad(string filename)
{
var content = XFile.GetFile(filename);
return File(content, System.Net.Mime.MediaTypeNames.Application.Zip, filename);
}
Get File is an extention method:
public static byte[] GetFile(string name)
{
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
string filenanme = path + "/" + name;
byte[] bytes = File.ReadAllBytes(filenanme);
return bytes;
}
Home controller Index view looks like this:
@model List
@Html.DisplayName("File Name")
@Html.DisplayName("Last Write Time")
@Html.DisplayName("Length (mb)")
@foreach (var item in Model)
{
@Html.ActionLink("DownLoad","DownLoad",new {filename=item.Name})
@Html.DisplayFor(modelItem => item.Name)
@Html.DisplayFor(modelItem => item.LastWriteTime)
@Html.DisplayFor(modelItem => item.Length)
}
The main index file action method:
public ActionResult Index()
{
var names = XFile.GetFileInformation();
return View(names);
}
Where GetFileInformation is an extension method:
public static List GetFileInformation()
{
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
var dirInfo = new DirectoryInfo(path);
return dirInfo.EnumerateFiles().ToList();
}