mvc控制器中的下载文件路径出错



这是我在索引视图上的操作链接。

@Html.ActionLink("Download File", "Download", new { fileName = Model.OriginalRecordRelativeFilePath })

public FileResult Download(string fileName)
{
     try
     {
          var fs = System.IO.File.OpenRead(Server.MapPath(ConfigurationHelper.IntegrationInterfaceFolderPath + fileName));
     // ConfigurationHelper.IntegrationFolderPath is a path for C:DataIntergrationInterface
     return File(fs, "application/octet-stream", fileName);
 }
 catch
 {
      throw new HttpException(404, "Couldn't find " + fileName);
 }

}

我尝试从C:\Data\IntergrationInterface\fileName下载该文件。但它找不到C:\Data\IntergrationInterface\fileName的路径并抛出异常(Couldnt find fileName)。我可以知道在同一台计算机上运行localhost web服务时,有什么方法可以从本地c下载文件吗?非常感谢。

此处不要使用Server.MapPath,而是直接使用文件路径。例如@"C:\Data\IntergrationInterface\"。

目前你正在尝试Server.MapPath(@"C:\Data\IntergrationInterface\file.txt"),这意味着你正在提供一个物理路径,而MapPath方法需要一个虚拟路径。相反,如果你给Server.MapPath("\ABC"),它会返回服务器路径为"C:\inetpub\wwwroot\ABC",这基本上是你的本地服务器路径。

最新更新