Jan*_*Jan 3 asp.net asp.net-mvc file-io file asp.net-mvc-2
在我的ASP.NET MVC应用程序中,我正在生成excel报告,我有一个模板文件,我可以复制和更改.此模板文件放在我的解决方案的文件夹中.我想用它如下:
string templatePath = @"\Templates\report.xlsx";
using (var template = File.OpenRead(templatePath)) {
// Copy template and process content
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码会产生异常
Couldnot find a part of the path 'C:\Templates\report.xlsx'.
Run Code Online (Sandbox Code Playgroud)
我该如何引用此文件?
我也试过用
string templatePath = @"~\Templates\report.xlsx";
Run Code Online (Sandbox Code Playgroud)
但结果是
Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\Templates\report.xlsx'.
Run Code Online (Sandbox Code Playgroud)
但是当我使用绝对路径但它对我的生产服务器毫无意义时,它确实有效.
我相信你会采用正常的ASP.NET方式,假设Templates是你的web应用程序中的一个目录.
string templatePath = @"~\Templates\report.xlsx";
using (var template = File.OpenRead(Server.MapPath(templatePath))) {
// Copy template and process content
}
Run Code Online (Sandbox Code Playgroud)