如何在 asp.net mvc 中使用 DownloadProgressChangedEventHandler?
How Can I Use DownloadProgressChangedEventHandler in asp.net mvc?
我有一个网站,我想用 WebClient Class 下载文件。
例如,我 url 想要下载它。在控制台应用程序中,此方法和代码可以正常工作。
这是控制台应用程序中的示例代码:
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);
}
此示例代码在控制台应用程序中可以正常工作。
如何在 asp.net mvc 应用程序中使用此示例代码。
对于 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.mp3");
return View();
}
对我来说,事件处理程序方法 (Downloader_DownloadProgressChanged) 它非常重要,因为我想在客户端创建进度条。
因为我认为这是最好的方法。但是怎么可能呢?
您可以通过将此代码添加到您的项目来实现此目的:
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);
}
我有一个网站,我想用 WebClient Class 下载文件。
例如,我 url 想要下载它。在控制台应用程序中,此方法和代码可以正常工作。
这是控制台应用程序中的示例代码:
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);
}
此示例代码在控制台应用程序中可以正常工作。
如何在 asp.net mvc 应用程序中使用此示例代码。
对于 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.mp3");
return View();
}
对我来说,事件处理程序方法 (Downloader_DownloadProgressChanged) 它非常重要,因为我想在客户端创建进度条。
因为我认为这是最好的方法。但是怎么可能呢?
您可以通过将此代码添加到您的项目来实现此目的:
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);
}