public class ThreadDownloadFile
{
public string url;
public string localPath;
public bool isDone = false;
public ThreadDownloadFile(string url, string localPath)
{
this.url = url;
this.localPath = localPath;
}
public void Download()
{
long lastDownPos = 0;
FileStream fs;
if (File.Exists(localPath))
{
fs = File.Open(localPath, FileMode.Append);
lastDownPos = fs.Length;
}
else
{
fs = File.Create(localPath);
}
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
if (lastDownPos > 0)
{
request.AddRange((int)lastDownPos);
}
using (Stream stream = request.GetResponse().GetResponseStream())
{
int nReadSize = 0;
do
{
byte[] buffer = new byte[1024 * 100];
nReadSize = stream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, nReadSize);
} while (nReadSize > 0);
}
fs.Flush();
fs.Close();
isDone = true;
}
ThreadDownloadFile downFile = null;
string localPath;
public void OnGUI()
{
if (GUILayout.Button("Download"))
{
string url = "http://xiazai.xiazaiba.com/Soft/F/FSCapture_8.4_XiaZaiBa.zip";
localPath = Application.persistentDataPath+"/"+Path.GetFileName(url);
downFile = new ThreadDownloadFile(url, localPath);
Thread thread = new Thread(downFile.Download);
thread.Start();
}
if (downFile!=null&&downFile.isDone)
{
GUILayout.Label("Complete");
}
}