[英]How Can I Use DownloadProgressChangedEventHandler in asp.net mvc?
I have website that I want download file with WebClient Class . 我有网站,我想用WebClient Class下载文件。
For example I have url that I want download it . 例如,我有网址,我想下载它。 in console application this methods and code work correctly .
在控制台应用程序中,此方法和代码正常工作
This is sample code in Console application : 这是控制台应用程序中的示例代码:
public void DownloadFile(string sourceUrl, string targetFolder)
{
WebClient downloader = new WebClient();
downloader.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(Downloader_DownloadFileCompleted);
downloader.DownloadProgressChanged +=
new DownloadProgressChangedEventHandler(Downloader_DownloadProgressChanged);
downloader.DownloadFileAsync(new Uri(sourceUrl.Replace(@"\","")), targetFolder);
while (downloader.IsBusy) { }
}
private void Downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//Console.Write(e.BytesReceived + " " + e.ProgressPercentage);
Console.Write("%" + e.ProgressPercentage);
}
this sample code work correctly in console application . 此示例代码在控制台应用程序中正常工作
how can i use this sample code in asp.net mvc application . 我如何在asp.net mvc应用程序中使用此示例代码。
for asp.net mvc it should like this (i think) 对于asp.net mvc它应该喜欢这个(我认为)
public ActionResult DownloadPage()
{
string url = "https://rjmediamusic.com/media/mp3/mp3-256/Mostafa-Yeganeh-Jadeh.mp3";
var downld = new DownloadManager();
downld.DownloadFile(url, @"c:\\temp\1.mp3");
return View();
}
for me the event handler method (Downloader_DownloadProgressChanged) it very important to work because i want create progress bar in client side . 对我来说,事件处理程序方法(Downloader_DownloadProgressChanged)非常重要,因为我想在客户端创建进度条。
As i think this is best way to do that . 我认为这是最好的方法。 but how can ?
但怎么可以?
You can achieve this by adding this code to your project: 您可以通过将此代码添加到项目中来实现此目的:
public void DownloadFile(string sourceUrl, string targetFolder)
{
WebClient downloader = new WebClient();
downloader.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(Downloader_DownloadFileCompleted);
downloader.DownloadProgressChanged +=
new DownloadProgressChangedEventHandler(Downloader_DownloadProgressChanged);
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(Downloader_DownloadFileCompleted);
downloader.DownloadProgressChanged +=
new DownloadProgressChangedEventHandler(Downloader_DownloadProgressChanged);
downloader.DownloadFileAsync(new Uri(sourceUrl.Replace(@"\","")), targetFolder);
while (downloader.IsBusy) { }
}
private void Downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//Console.Write(e.BytesReceived + " " + e.ProgressPercentage);
Console.Write("%" + e.ProgressPercentage);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.