我已经建立了一个网站,其中有一个名为Zipfile
的文件夹,我想从中访问LSC.exe
文件进行下载。当我点击路径时它会给出以下异常System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\IIS Express\~\Zipfile\LSC.exe'.
请给我任何建议。
调节器
[HttpGet]
public FileResult downloadFile()
{
byte[] fileBytes = System.IO.File.ReadAllBytes("~/Zipfile/LSC.exe");
string fileName = "SkypeSetup.exe";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
您需要添加Server.MapPath(exe路径)以获取映射到其相对路径
byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Zipfile/LSC.exe"));
您应该使用Server.MapPath映射到应用程序目录下的文件。
试试以下代码:
public FileResult downloadFile()
{
byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Zipfile/LSC.exe"));
string fileName = "SkypeSetup.exe";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}