[英]DownloadFile in ASP mvc 4 from physical path
I need to make downloading files from the server .我需要从服务器下载文件。 Server is specified physical path.服务器是指定的物理路径。 the file is downloaded , but there are problems that I do not know how to fix .文件已下载,但有一些我不知道如何解决的问题。 1) All the files are named as "Download".2) does not have an extension . 1) 所有文件都命名为“下载”。2) 没有扩展名。 Help me, please, I'm new to ASP MVC请帮助我,我是 ASP MVC 的新手
Code of Controller :控制器代码:
public ActionResult NIRInfo()
{
List<string> filesList = new List<string>();
var dir = new System.IO.DirectoryInfo(@"Z:\Docs");
System.IO.FileInfo[] fileNames = dir.GetFiles("*.*");
var xow = from i in db.NIRs where i.Id == id select i.File;
foreach (var i in xow)
{
fileNames = dir.GetFiles(i);
foreach (var f in fileNames)
{
filesList.Add(f.Name);
}
ViewData["fList"] = filesList;
}
return View(nir);
}
public FileResult Download(string Name)
{
return File(@"Z:\Docs\" + Name, MediaTypeNames.Application.Octet);
}
Code of view视图代码
@{ var fList = ViewData["fList"] as List<string>;}
@if (ViewData["fList"] != null)
{
<table>
@foreach (var f in fList)
{
<tr>
<td>
<a href="@Url.Action("Download", "NIR", new { Name = @f })">@f</a>
</td>
</tr>
}
</table>
}
You need to specify a filename in method overload.您需要在方法重载中指定文件名。 Look at third parameter in File
method查看File
方法中的第三个参数
public FileResult Download(string Name)
{
return File(@"Z:\Docs\" + Name, MediaTypeNames.Application.Octet, "document.txt");
}
HttpResponseBase response;
response.Clear();
response.AddHeader("content-disposition", "attachment;filename=\"" + fileName + "\"");
response.TransmitFile(Path.Combine(FileName, Path.GetFileName(fileName )));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.