在我的 ASP.NET MVC 应用程序中,我正在生成 Excel 报告,我有一个可以复制和更改的模板文件。该模板文件放在我的解决方案中的一个文件夹中。我想按如下方式使用它:
string templatePath = @"\Templates\report.xlsx";
using (var template = File.OpenRead(templatePath)) {
// Copy template and process content
}
但是这段代码会产生异常
Couldnot find a part of the path 'C:\Templates\report.xlsx'.
我应该如何引用这个文件?
我也尝试过使用
string templatePath = @"~\Templates\report.xlsx";
但这会导致
Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\Templates\report.xlsx'.
但是当我使用绝对路径时它确实有效,但这对我的生产服务器毫无意义。
最佳答案
我相信您会以正常的 ASP.NET 方式执行此操作,假设 Templates 是您的 Web 应用程序中的一个目录。
string templatePath = @"~\Templates\report.xlsx";
using (var template = File.OpenRead(Server.MapPath(templatePath))) {
// Copy template and process content
}
关于asp.net - 在 ASP.NET MVC 中访问服务器上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4107493/