WebClient.DownloadFile不起作用

时间:2015-01-27 13:14:00

标签: c# downloadfile

我正在尝试从我的服务器下载文件,我这样做:

var client = new WebClient();
client.DownloadFile(@"C:\Files\project\result.xlsx", "download.xlsx");

并且例外说明了这一点:

2015-01-27 13:08:13,686 [55] ERROR GeneralLogger - System.Net.WebException: An exception occurred during a WebClient request. ---> System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\IIS Express\download.xlsx' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at System.Net.WebClient.DownloadFile(String address, String fileName)
   at EmailTemplates.Classes.ExcelUtil.CreateNewExcelFile(String variables) in 

我从未使用路径C:\Program Files (x86)\IIS Express\download.xlsx可能是默认路径或类似的东西。 我想在任何页面上点击下载时将文件下载(我这样做是因为我在同一个函数中创建了文件)

感谢。

1 个答案:

答案 0 :(得分:3)

我认为你误解了DownloadFile的作用。它不会文件发送给您的网络应用程序的客户端。它从服务器下载文件。由于您没有指定任何URL,我认为这是您的错误。

研究如何将文件作为对HTTP请求的响应发送。我会搜索“ASP.NET下载文件”。