Gre*_*reg 5 c# asp.net webclient-download
我试图设置一个按钮,以便用户可以下载保存在服务器上的文件。
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");
}
Run Code Online (Sandbox Code Playgroud)
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");
Run Code Online (Sandbox Code Playgroud)
如果我换了
webclient.DownloadFile(@"D:\web\mytestwebsite.com\www\temp\test.html", "test.html");
Run Code Online (Sandbox Code Playgroud)
要么
webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), "test.html");
Run Code Online (Sandbox Code Playgroud)
我获得对路径'C:\ Windows \ SysWOW64 \ inetsrv \ test.html'的访问被拒绝。
最后,我进入文件夹C:\ Windows \ SysWOW64 \ inetsrv,授予NETWORK SERVICE权限,但是它说访问被拒绝。我在服务器上以管理员身份登录。
我读了一些关于此的文章,但似乎没有任何作用,或者我错过了一些东西。
什么是使用WebcClient.DownloadFile的正确方法?
让您的问题引起我的注意。当您使用webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), @"C:\web\test.html");,那么请确保您有目录web在C:\。我的意思是确保C:\web文件系统中有文件。否则会引发错误。因为它需要目录存在,所以它才可以创建新文件。
完成后 webclient.DownloadFile(@"D:\web\mytestwebsite.com\www\temp\test.html", "test.html");,它尝试在当前执行文件范围内C:\Windows\SysWOW64\inetsrv\创建文件,即由于访问权限,它尝试在此处创建文件,因此无法在此处创建文件。
解决方案,尝试使用以下代码,应该在中创建文件D:\web。
webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), @"D:\web\test.html");
编辑
免责声明您做错了(如果我不清楚要求),DownloadFile是将URL下载到服务器位置,现在可以下载本地文件并将其保存到本地位置,就像将save as文件保存到其他任何位置一样。
编辑
要允许最终用户下载文件,请使用以下代码。
Response.Clear();
Response.ContentType = "text/html";
Response.AddHeader("Content-Disposition", "attachment;filename=a.html"); // replace a.html with your filename
Response.WriteFile(@"D:\webfile\a.html"); //use your file path here.
Response.Flush();
Response.End();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12702 次 |
| 最近记录: |