【发布时间】:2011-05-02 16:58:34
【问题描述】:
我将数据库中的文件路径存储为 ~/FolderName/FileName 并且当我尝试以这种方式使用 System.IO.FileInfo(filePath) 打开文件时。它无法破译文件路径。此外,我在类 DownloadFile 中使用此语句,因此我无法使用 Page.Server.MapPath。有没有办法解决这个问题。
这些是我正在使用的以下代码行:
if (dr1.Read())
{
String filePath = dr1[0].ToString();
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String disHeader = "Attachment; Filename=\"" + fileName + "\"";
HttpContext.Current.Response.AppendHeader("Content-Disposition", disHeader);
System.IO.FileInfo fileToDownload = new System.IO.FileInfo(filePath);
string fullName = fileToDownload.FullName;
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.WriteFile(fileToDownload.FullName);
sqlCon.Close();
}
文件路径的格式为~/ComponentFolderForDownloading/FileName.exe
我该如何解决这个问题?
【问题讨论】:
-
有什么理由不能在 downloadFile 类中使用 MapPath?
-
@Joe 有人可以向我解释一下 Server.MapPath("~/ComponentFolder/File1.exe") 和 Server.MapPath("~\ComponentFolder\File1.exe") 有什么区别用这个 / 和 \ ??
标签: c# asp.net relative-path