WebRequest 方式 文件下载
2012-11-21 20:49
330 查看
使用 WebRequest 直接下载会阻塞UI线程,要么使用多线程,要么使用异步方式
1)首先创建一个连接请求
string fullURL=@"http://neirong.funshion.com/software/download.php?id=6930&f=FunshionInstall2.6.6.43Beta.exe";
WebRequest webReq = WebRequest.Create(fullURL);
下面是详细的下载
View Code
这个是使用一个 System.Threading.ThreadPool.QueueUserWorkItem(Down, new object()); } 进行下载,
1)首先创建一个连接请求
string fullURL=@"http://neirong.funshion.com/software/download.php?id=6930&f=FunshionInstall2.6.6.43Beta.exe";
WebRequest webReq = WebRequest.Create(fullURL);
try { WebResponse webRes = webReq.GetResponse(); long fileLength = webRes.ContentLength; Stream srm = webRes.GetResponseStream(); StreamReader srmReader = new StreamReader(srm); byte[] bufferbyte = new byte[fileLength]; int MaxPro = (int)bufferbyte.Length; progressBar1.Dispatcher.Invoke(new Action(()=>{ progressBar1.Maximum = MaxPro; })); int Currentvalue = 0; while (fileLength > 0) { int downByte = srm.Read(bufferbyte, Currentvalue, MaxPro); if (downByte == 0) { break; }; Currentvalue += downByte; progressBar1.Dispatcher.Invoke(new Action(() => { progressBar1.Value = Currentvalue; })); MaxPro -= downByte; float part = (float)Currentvalue / 1024; float total = (float)bufferbyte.Length / 1024; int percent = Convert.ToInt32((part / total) * 100); } string tempPath = System.IO.Path.Combine(System.Environment.CurrentDirectory, "测试下载"); CreateDirtory(tempPath); FileNameInfo = System.IO.Path.Combine(tempPath, FileNameInfo); FileStream fs = new FileStream(FileNameInfo, FileMode.OpenOrCreate, FileAccess.Write); fs.Write(bufferbyte, 0, bufferbyte.Length); srm.Close(); srmReader.Close(); fs.Close(); } catch { Console.WriteLine("下载文件异常"); } }
下面是详细的下载
View Code
<Window x:Class="WpfApplication1.HttpdownLoadxaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="HttpdownLoadxaml" Height="300" Width="300"> <Grid> <ProgressBar Height="10" Minimum="0" HorizontalAlignment="Left" Margin="12,52,0,0" Name="progressBar1" VerticalAlignment="Top" Width="243" /> <Button Content="开始下载" Height="23" HorizontalAlignment="Left" Margin="41,207,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" Click="btnStart_Click" /> <Button Content="暂停" Height="23" HorizontalAlignment="Right" Margin="0,207,31,0" Name="btnStop" VerticalAlignment="Top" Width="75" Click="btnStop_Click" /> </Grid> </Window>
这个是使用一个 System.Threading.ThreadPool.QueueUserWorkItem(Down, new object()); } 进行下载,
相关文章推荐
- HttpWebRequest 下载网页Html代码 下载文件(Remote和FTP)Get方式
- Windows Mobile Web方式下载文件
- 使用 HttpWebRequest 下载任意类型的文件
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- HttpWebRequest下载文件带进度条
- .Net 使用HttpWebRequest 和 HttpWebResponse 下载文件
- Windows Mobile Web方式下载文件
- 上传文件一直用web_custon_request这种方式上传流文件,其实也可以用表单的方式上传文件,只需要将文件放在LR文件包中的目录。
- 在Windows Mobile 中利用 WebRequest 下载文件并获得响应头的信息信息
- UnityWebRequest下载网络资源,支持断点续传、多文件同时下载
- 使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件
- FtpWebRequest FTP异步下载、异步上传文件
- 在Windows Mobile中利用WebRequest下载文件并获得响应头的信息信息
- httpwebrequest下载文件失败的解决方案
- Windows Mobile Web方式下载文件
- 使用.net FtpWebRequest 实现FTP常用功能 上传 下载 获取文件列表 移动 切换目录 改名
- 文件下载--getRequestDispatcher以及文件流输出的方式
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- Windows Mobile Web方式下载文件