【发布时间】:2014-12-18 03:16:54
【问题描述】:
我正在尝试设置一个按钮,以便用户可以下载保存在服务器上的文件。
protected void btnDownloadHtmlFile_Click(object sender, EventArgs e)
{
string path = @"D:\web\mytestwebsite.com\www\temp\test.html";
if (!File.Exists(path))
{
File.Create(path);
}
TextWriter tw = new StreamWriter(path);
tw.WriteLine("<head></head><body>test</body>");
tw.Close();
WebClient webclient = new WebClient();
webclient.DownloadFile(@"D:\web\mytestwebsite.com\www\temp\test.html", @"C:\web\test.html");
}
这导致Could not find a part of the path 'C:\web\test.html'.
如果我改变了,也一样
webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), @"C:\web\test.html");
如果我换了
webclient.DownloadFile(@"D:\web\mytestwebsite.com\www\temp\test.html", "test.html");
或
webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), "test.html");
我无法访问路径“C:\Windows\SysWOW64\inetsrv\test.html”。
最后我去了文件夹 C:\Windows\SysWOW64\inetsrv 授予 NETWORK SERVICE 权限,但它说访问被拒绝。我在服务器上以管理员身份登录。
我阅读了一些关于此的帖子,但似乎没有任何效果,或者我错过了一些东西。
WebcClient.DownloadFile 的正确使用方法是什么?
【问题讨论】:
-
首先,为什么需要在服务器位置创建文件并再次将其保存到服务器位置?
标签: c# asp.net webclient-download