【问题标题】:WebClient.DownloadFile doesn't workWebClient.DownloadFile 不起作用
【发布时间】: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。第二个是保存到本地路径。

标签: c# webclient


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2021-10-15
    • 2019-01-26
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多