-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileDownloader.cs
115 lines (105 loc) · 3.38 KB
/
FileDownloader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Forms;
public class FileDownloader
{
private Form _downloadingform;
private ProgressBar _progressbar;
private bool goPause;
private string LocalPath;
private static int PercentProgress;
private Stream strLocal;
private Stream strResponse;
private Thread thrDownload;
private string Url;
private HttpWebRequest webRequest;
private HttpWebResponse webResponse;
public FileDownloader(Form DownloadingForm, ProgressBar pb)
{
Downloading_Form = DownloadingForm;
Progress_Bar = pb;
}
private void Download(object startPoint)
{
try
{
int num3;
int range = Convert.ToInt32(startPoint);
webRequest = (HttpWebRequest) WebRequest.Create(Url);
webRequest.AddRange(range);
webRequest.Credentials = CredentialCache.DefaultCredentials;
webResponse = (HttpWebResponse) webRequest.GetResponse();
long contentLength = webResponse.ContentLength;
strResponse = webResponse.GetResponseStream();
strLocal = (range == 0) ? new FileStream(LocalPath, FileMode.Create, FileAccess.Write, FileShare.None) : new FileStream(LocalPath, FileMode.Append, FileAccess.Write, FileShare.None);
byte[] buffer = new byte[0x800];
while ((num3 = strResponse.Read(buffer, 0, buffer.Length)) > 0)
{
strLocal.Write(buffer, 0, num3);
Downloading_Form.Invoke(new UpdateProgessCallback(UpdateProgress), new object[] { strLocal.Length, contentLength + range });
if (goPause)
{
return;
}
}
}
finally
{
strResponse.Close();
strLocal.Close();
}
}
public byte[] DownloadData(string url)
{
DownloadFile(url, Application.StartupPath + @"\tempdata.temp");
BinaryReader reader = new BinaryReader(new FileStream(Application.StartupPath + @"\tempdata.temp", FileMode.Open));
byte[] buffer = reader.ReadBytes((int) reader.BaseStream.Length);
reader.Close();
System.IO.File.Delete(Application.StartupPath + @"\tempdata.temp");
return buffer;
}
public void DownloadFile(string url, string Path)
{
Url = url;
LocalPath = Path;
thrDownload = new Thread(new ParameterizedThreadStart(Download));
thrDownload.Start(0);
while (thrDownload.ThreadState != ThreadState.Stopped)
{
Application.DoEvents();
}
}
private void UpdateProgress(long BytesRead, long TotalBytes)
{
PercentProgress = Convert.ToInt32((long) ((BytesRead * 100L) / TotalBytes));
Progress_Bar.Maximum = 100;
Progress_Bar.Minimum = 0;
Progress_Bar.Value = PercentProgress;
}
public Form Downloading_Form
{
get
{
return _downloadingform;
}
set
{
_downloadingform = value;
}
}
public ProgressBar Progress_Bar
{
get
{
return _progressbar;
}
set
{
_progressbar = value;
}
}
private delegate void UpdateProgessCallback(long BytesRead, long TotalBytes);
}