我在asp.net有一个网站。我把它托管在我的服务器上。我的服务器有3个名为C,D,E的目录。我的Asp.net应用程序是在C盘中。在模块中,我上传了一些存储在My Server D目录中的图像。现在我想从asp.net代码访问它,但它显示一条错误消息: “asp.net是一条物理路径,但预计会有虚拟路径”。如何从此场景访问我的图像。请帮帮我....我的代码如下..
ImageButton lnkbtn = sender as ImageButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
string fileName = grvImpact.DataKeys[gvrow.RowIndex].Values[3].ToString();
string filePath = "D://Upload//CRDocument//" + fileName.ToString();
if (fileName != null)
{
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(filePath);
Response.End();
}
它不起作用......
答案 0 :(得分:2)
使用Server.MapPath指定映射到物理目录的相对或虚拟路径。
string FilePath;
FilePath = Server.MapPath("/MyWebSite");
答案 1 :(得分:0)
让我们说你有
URL
www.mydomain.com/D/images/MyMegaImage.jpg
文件系统
c:\inetpub\mywebsite\wwwroot\D\images\MyMegaImage.jpg
在代码中你会有像
这样的东西string imageFileName = "MyMegaImage.jpg";
string FullFilePath = HttpContext.Current.Server.MapPath("/D/images/" + imageFileName);
然后变量FullFilePath应该在“c:\ inetpub \ mywebsite \ wwwroot \ D \ images \ MyMegaImage.jpg”中计算