【发布时间】:2015-01-27 13:14:00
【问题描述】:
我正在尝试从我的服务器下载一个文件,我这样做了:
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 路径,可能是默认路径或类似的路径。
我想在任何页面上单击下载时下载文件(我这样做是因为我在同一函数中创建文件)
谢谢。
【问题讨论】:
-
您的第一个参数应该是您希望下载的远程文件的 URL。第二个是保存到本地路径。