简体   繁体   English

如何将文件从Web下载到ASP.NET Web应用程序中?

[英]How to download files from web into an ASP.NET Web Application?

I'm trying to download a file I recieve on the web into my ASP.NET web application, inside a seperate folder. 我正在尝试将我在网上收到的文件下载到单独文件夹内的ASP.NET Web应用程序中。 Is it possible to do this? 是否有可能做到这一点? I tried using a WebClient to download the file but got an exception for InvalidOperationException. 我尝试使用WebClient下载文件,但InvalidOperationException出现异常。

InvalidOperationException was unhandled by user code 用户代码未处理InvalidOperationException

Asynchronous operations are not allowed in this context. 在这种情况下,不允许异步操作。 Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event. 页面启动异步操作必须将Async属性设置为true,并且异步操作只能在PreRenderComplete事件之前的页面上启动。

And here's the code that causes the exception: 这是导致异常的代码:

WebClient wc = new WebClient();
wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(new Uri("research.microsoft.com/pubs/70177/tr-2005-68.pdf", UriKind.RelativeOrAbsolute), Server.MapPath("FileUploads/download.pdf"));

The following MSDN Article Asynchronous Pages in ASP.NET 2.0 does a nice job of explaining how you can handle async operations on a web page. 以下ASP.NET 2.0中的MSDN文章“ 异步页面”很好地解释了如何处理网页上的异步操作。 The sample code in fact uses a WebRequest to download from http://msdn.microsoft.com 实际上,该示例代码使用WebRequest从http://msdn.microsoft.com下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM