我正在使用从服务器下载一些文件的DownloadFileAsync
方法WebClient
,我不禁注意到在VS2010中我的代码的非正式测试中,它在启动时会阻塞大约3秒钟,在我看来,首先打败了目的.
以下是相关的代码片段:
WebClient downloader = new WebClient();
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(updateDownloadProgress);
downloader.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(downloadCompleted);
var current_map = map_downloads[0];//string with filename, map_downloads is List<string>
var path = System.IO.Path.GetTempFileName();
downloaded_maps.Add(path);//adding the temp file to a List<string>
downloader.DownloadFileAsync(new Uri(MAP_BASE + current_map), path); //MAP_BASE is a string containing the base url
Run Code Online (Sandbox Code Playgroud)
DownloadFileAsync
当应用程序下载~100 MB文件时,我正在使用UI阻止阻止.显然,如果UI在呼叫开始时阻塞了3秒,那么即使不完全,也会稍微减少效用.
我对C#/ .Net相对缺乏经验(大约3 - 4年前我做了一堆.Net 2.0的东西,IIRC,但我现在基本上重新学习它).
美好的一天.我正在使用DownloadFileAsync处理文件下载器类.在正常情况下一切正常.但是当我下载文件并禁用网络连接时,下载进度只是停止无限时间,不会引发任何错误或调用任何回调.任何想法如何处理这种情况?非常感谢.
_client.Proxy = WebRequest.DefaultWebProxy;
_client.DownloadProgressChanged += (sender, argv) => { actionCallback(argv.ProgressPercentage); }
_client.DownloadFileCompleted += (sender, argv) => {
if (argv.Error != null) {
_exc = argv.Error;
}
set event
}
Task.Factory.StartNew(() => {
_client.DownloadFileAsync(request, targetFileName);
thread sync
if (_exc != null) {
logger.ErrorException(exc);
throw;
}
Run Code Online (Sandbox Code Playgroud)
问题出现在Vista和2k8下.在Win7上一切正常.
我下载了一些文件,但我也想为webclient设置超时.我看到没有变化只是我们可以使用重写WebRequest.我已经做了但它不起作用.我的意思是重写GetWebRequest方法不起作用..这是我的代码
public class VideoDownloader : Downloader
{
/// <summary>
/// Initializes a new instance of the <see cref="VideoDownloader"/> class.
/// </summary>
/// <param name="video">The video to download.</param>
/// <param name="savePath">The path to save the video.</param>
public VideoDownloader(VideoInfo video, string savePath)
: base(video, savePath)
{ }
/// <summary>
/// Starts the video download.
/// </summary>
public override void Execute()
{
// We need a handle to keep the method synchronously
var handle = new ManualResetEvent(false);
var client = new WebClient();
client.DownloadFileCompleted …
Run Code Online (Sandbox Code Playgroud) 我正在尝试同时下载多个文件。如何确定并行下载的最佳文件数量?
WebClient DownloadFileAsync() 不适用于相同的 URl 和凭据...
有什么线索吗?
static void Main(string[] args)
{
try
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
// It works fine.
client.DownloadFile(urlAddress, @"D:\1.xlsx");
}
/*using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
// It y creats file with 0 bytes. Dunow why is it.
client.DownloadFileAsync(new Uri(urlAddress), @"D:\1.xlsx");
//client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
}*/
}
catch (Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个WPF应用程序,我想下载一个文件.
我正在使用System.Net; 我有以下代码:
WebClient ww = new WebClient();
ww.DownloadFileAsync(
new Uri("http://www.sinvise.net/tester/1.jpg"),
AppDomain.CurrentDomain.BaseDirectory + "\\1.jpg");
Run Code Online (Sandbox Code Playgroud)
问题是,它是不下载文件,它只是显示为0kb文件而不是下载,我不知道是什么问题,任何人都可以帮忙吗?
我正在尝试test.exe
使用以下代码下载文件:
public void DownloadFile()
{
using(var client = new WebClient())
{
client.DownloadFileAsync(new Uri("http://www.acromix.net16.net/download/test.exe"), "test.exe");
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道,也不知道为什么它是0 KB(应该是328 KB).[ / downloads ]
我怎样才能使它工作?
编辑:
托管网站(000webhost)阻止.exe
文件下载...
c# download downloadfileasync async-await webclient-download
在开发允许用户检查新应用程序更新的功能时,我已经呆了好几天(我使用本地服务器作为分发点)。问题是下载进度似乎工作正常,但我在手机的任何位置都找不到下载的文件(我没有SD卡/外部存储器)。以下是我到目前为止所取得的成就。
class DownloadFileFromURL extends AsyncTask<String, String, String> {
ProgressDialog pd;
String path = getFilesDir() + "/myapp.apk";
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(DashboardActivity.this);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setMax(100);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(true);
//pd.setIndeterminate(true);
pd.show();
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(path);
byte data[] …
Run Code Online (Sandbox Code Playgroud) 我正在使用WebClient.DownloadFileAsync
获取一批文件.但是有些文件不完整,没有例外.
我的问题是,如何在下载的文件未完成时进行标记?没有md5校验和来验证.
代码段是:
using (WebClient client = new WebClient())
{
Uri sUri = new Uri(sFileLink);
client.DownloadFileAsync(sUri, myPath);
}
Run Code Online (Sandbox Code Playgroud) c# ×8
webclient ×6
.net ×2
download ×2
.net-4.0 ×1
android ×1
async-await ×1
hang ×1
performance ×1
timeout ×1
webrequest ×1