以下是下载文件的代码。我的网址工作(能够下载),但我的url2无法正常工作(无法下载)。我为url2做了什么错误?我的相对路径有什么问题吗?
protected void Button1_Click(object sender, EventArgs e)
{
string url =@"C:\Users\Roy\Desktop\backup fyp\10-18-2011\WebSite5\123.txt";
string url2 = @"~\123.txt";
FileInfo finfo = new FileInfo(url);
if (finfo.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + finfo.Name);
Response.AddHeader("Content-Length", finfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.WriteFile(finfo.FullName);
}
else
{
Response.Write("error");
}
}
答案 0 :(得分:2)
~\123.txt
是一条虚拟路径。你需要像这样使用它:
string url2 = HttpContext.Current.Server.MapPath(@"~\123.txt");