对于那些只想从App_Data文件夹返回现有Zip文件的人(只需将zip文件放在其中),在Home控制器中创建此操作方法:
public FileResult DownLoad(string filename)
{
var content = XFile.GetFile(filename);
return File(content, System.Net.Mime.MediaTypeNames.Application.Zip, filename);
}
Get File 是一个扩展方法:
public static byte[] GetFile(string name)
{
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
string filenanme = path + "/" + name;
byte[] bytes = File.ReadAllBytes(filenanme);
return bytes;
}
主页控制器的索引视图如下:
@model List<FileInfo>
<table class="table">
<tr>
<th>
@Html.DisplayName("File Name")
</th>
<th>
@Html.DisplayName("Last Write Time")
</th>
<th>
@Html.DisplayName("Length (mb)")
</th>
<th></th>
</tr>
@foreach (var item in Model)
)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastWriteTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.Length)
</td>
</tr>
}
</table>
主索引文件操作方法:
public ActionResult Index()
{
var names = XFile.GetFileInformation();
return View(names);
}
GetFileInformation是一个扩展方法:
public static List<FileInfo> GetFileInformation()
{
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
var dirInfo = new DirectoryInfo(path);
return dirInfo.EnumerateFiles().ToList();
}