Pisze program do pobierania plikow. Pliki pobieram w ten sposob:
// the URL to download the file from
string sUrlToReadFileFrom = _dl;
// the path to write the file to
Uri _uri = new Uri(_dl);
string sFilePathToWriteFileTo = _path+Path.GetFileName(_uri.AbsolutePath);
// first, we need to get the exact size (in bytes) of the file we are downloading
Uri url = new Uri(sUrlToReadFileFrom);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
// gets the size of the file in bytes
Int64 iSize = response.ContentLength;
// keeps track of the total bytes downloaded so we can update the progress bar
Int64 iRunningByteTotal = 0;
// use the webclient object to download the file
using (System.Net.WebClient client = new System.Net.WebClient())
{
// open the file at the remote URL for reading
using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
{
// using the FileStream object, we can write the downloaded bytes to the file system
using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
{
// loop the stream and get the file into the byte buffer
int iByteSize = 0;
byte[] byteBuffer = new byte[iSize];
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
// write the bytes to the file system at the file path specified
streamLocal.Write(byteBuffer, 0, iByteSize);
iRunningByteTotal += iByteSize;
// calculate the progress out of a base "100"
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
// update the progress bar
backgroundWorker1.ReportProgress(iProgressPercentage);
}
// clean up the file stream
streamLocal.Close();
}
// close the connection to the remote server
streamRemote.Close();
}
}
oczywiscie to nie caly kod ale najwzniejsza czesc pobierajaca plik z internetu. Program działa ale tylko dla plików nie wiekszych mniej wiecej 60mb przy probie pobrania pliku 70mb lub wiekszego pojawia sie taki komunikat w tym miejscu
iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length))
Unable to read data from the transport connection: Nie można przeprowadzić operacji na gnieździe, ponieważ w systemie brakuje miejsca na bufor lub ponieważ została zapełniona kolejka.
podobno można jakoś buffer limit zmienić ale nie mogłem znaleźć nigdzie konkretnego przykładu więc nie wiem co mam zrobić aby mozna bylo pobrać wieksze pliki tak jak robi to przeglądarka, ma ktos pojecie jak mozna rozwiązać ten problem?
thx