我试图从我的web api返回文件 System.IO.FileNotFoundException Could not find file .
System.IO.FileNotFoundException Could not find file
我可以这样读取字节数组: System.IO.File.ReadAllBytes(csvPath);
System.IO.File.ReadAllBytes(csvPath);
但当我返回的文件路径相同时,如下所示: return File(csvPath, "text/csv", "data.csv");
return File(csvPath, "text/csv", "data.csv");
它在投掷 System.IO.FileNotFoundException找不到文件
System.IO.FileNotFoundException找不到文件
问题可能是 File 接受 virtualPath 作为一条路径,它不同于普通路径。阅读 this blog post 要了解有关ASP.NET中不同类型路径的更多信息,或者您可以使用接受文件流作为参数的不同重载: File .
File
virtualPath
例如:
var stream = System.IO.File.OpenRead(csvPath); return File(stream, "text/csv", "data.csv");