コード例 #1
0
ファイル: webdata.cs プロジェクト: EjaadTech/Ejaaduino
        public static bool downloadFromWeb(string URL, string file, string targetFolder)
        {
            try
            {

                byte[] downloadedData;

                downloadedData = new byte[0];

                //open a data stream from the supplied URL
                WebRequest webReq = WebRequest.Create(URL + file);
                WebResponse webResponse = webReq.GetResponse();
                Stream dataStream = webResponse.GetResponseStream();

                //Download the data in chuncks
                byte[] dataBuffer = new byte[1024];

                //Get the total size of the download
                int dataLength = (int)webResponse.ContentLength;

                //lets declare our downloaded bytes event args
                ByteArgs byteArgs = new ByteArgs();

                byteArgs.downloaded = 0;
                byteArgs.total = dataLength;

                //we need to test for a null as if an event is not consumed we will get an exception
                if (bytesDownloaded != null) bytesDownloaded(byteArgs);

                //Download the data
                MemoryStream memoryStream = new MemoryStream();
                while (true)
                {
                    //Let's try and read the data
                    int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);

                    if (bytesFromStream == 0)
                    {

                        byteArgs.downloaded = dataLength;
                        byteArgs.total = dataLength;
                        if (bytesDownloaded != null) bytesDownloaded(byteArgs);

                        //Download complete
                        break;
                    }
                    else
                    {
                        //Write the downloaded data
                        memoryStream.Write(dataBuffer, 0, bytesFromStream);

                        byteArgs.downloaded = bytesFromStream;
                        byteArgs.total = dataLength;
                        if (bytesDownloaded != null) bytesDownloaded(byteArgs);

                    }
                }

                //Convert the downloaded stream to a byte array
                downloadedData = memoryStream.ToArray();

                //Release resources
                dataStream.Close();
                memoryStream.Close();

                //Write bytes to the specified file
                FileStream newFile = new FileStream(targetFolder + file, FileMode.Create);
                newFile.Write(downloadedData, 0, downloadedData.Length);
                newFile.Close();

                return true;

            }

            catch (Exception)
            {
                //We may not be connected to the internet
                //Or the URL may be incorrect
                return false;
            }
        }
コード例 #2
0
ファイル: webdata.cs プロジェクト: eglrp/TESTPROJECT-1
        public static bool downloadFromWeb(string URL, string file, string targetFolder)
        {
            try
            {
                byte[] downloadedData;


                downloadedData = new byte[0];

                //open a data stream from the supplied URL
                WebRequest  webReq      = WebRequest.Create(URL + file);
                WebResponse webResponse = webReq.GetResponse();
                Stream      dataStream  = webResponse.GetResponseStream();

                //Download the data in chuncks
                byte[] dataBuffer = new byte[1024];

                //Get the total size of the download
                int dataLength = (int)webResponse.ContentLength;

                //lets declare our downloaded bytes event args
                ByteArgs byteArgs = new ByteArgs();

                byteArgs.downloaded = 0;
                byteArgs.total      = dataLength;

                //we need to test for a null as if an event is not consumed we will get an exception
                if (bytesDownloaded != null)
                {
                    bytesDownloaded(byteArgs);
                }


                //Download the data
                MemoryStream memoryStream = new MemoryStream();
                while (true)
                {
                    //Let's try and read the data
                    int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);

                    if (bytesFromStream == 0)
                    {
                        byteArgs.downloaded = dataLength;
                        byteArgs.total      = dataLength;
                        if (bytesDownloaded != null)
                        {
                            bytesDownloaded(byteArgs);
                        }

                        //Download complete
                        break;
                    }
                    else
                    {
                        //Write the downloaded data
                        memoryStream.Write(dataBuffer, 0, bytesFromStream);

                        byteArgs.downloaded = bytesFromStream;
                        byteArgs.total      = dataLength;
                        if (bytesDownloaded != null)
                        {
                            bytesDownloaded(byteArgs);
                        }
                    }
                }

                //Convert the downloaded stream to a byte array
                downloadedData = memoryStream.ToArray();

                //Release resources
                dataStream.Close();
                memoryStream.Close();

                //Write bytes to the specified file
                FileStream newFile = new FileStream(targetFolder + file, FileMode.Create);
                newFile.Write(downloadedData, 0, downloadedData.Length);
                newFile.Close();

                return(true);
            }

            catch (Exception)
            {
                //We may not be connected to the internet
                //Or the URL may be incorrect
                return(false);
            }
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: EjaadTech/Ejaaduino
        private void Bytesdownloaded(ByteArgs e)
        {
            progressBar1.Maximum = e.total;

            if (progressBar1.Value + e.downloaded <= progressBar1.Maximum)
            {
                progressBar1.Value += e.downloaded;
                SetLabel(line1, "Downloading Update...");
            }
            else
            {
                SetLabel(line1, "Download complete.");
            }

            progressBar1.Refresh();
            Invalidate();
        }