{"id":851,"date":"2012-11-22T18:46:24","date_gmt":"2012-11-22T18:46:24","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=851"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"download-file-from-url","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/tcp-ip\/download-file-from-url","title":{"rendered":"Download File From URL"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<pre>\r\n<code>\r\n\tprivate: WebClient ^WebClient1;\r\n\r\n\r\n\r\n\r\n\r\n\t\/\/*******************************\r\n\t\/\/*******************************\r\n\t\/\/********** FORM LOAD **********\r\n\t\/\/*******************************\r\n\t\/\/*******************************\r\n\tprivate: System::Void frmInstallControlCentre_Load(System::Object^  sender, System::EventArgs^  e)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\t\/\/----- CREATE DIRECTORY -----\r\n\t\t\tif (!Directory::Exists(TEMP_DOWNLOAD_DIRECTORY))\r\n\t\t\t\tDirectory::CreateDirectory(TEMP_DOWNLOAD_DIRECTORY);\r\n\r\n\r\n\t\t\t\/\/----- DELETE FILE IF IT ALREADY EXISTS -----\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tif (File::Exists(TEMP_DOWNLOAD_DIRECTORY + &quot;SomeFile.txt&quot;))\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/If file has read only attribute set clear it so that delete can occur\r\n\t\t\t\t\tif ((File::GetAttributes(TEMP_DOWNLOAD_DIRECTORY + &quot;SomeFile.txt&quot;) &amp; FileAttributes::ReadOnly) == FileAttributes::ReadOnly)\r\n\t\t\t\t\t\tFile::SetAttributes(TEMP_DOWNLOAD_DIRECTORY + &quot;SomeFile.txt&quot;, FileAttributes::Normal);\r\n\r\n\t\t\t\t\tFile::Delete(TEMP_DOWNLOAD_DIRECTORY + &quot;SomeFile.txt&quot;);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tcatch (IOException ^)\r\n\t\t\t{\r\n\t\t\t\tGlobalObjects::EventLog1-&gt;AddEvent(true, 0x00, &quot;Can&#39;t delete existing SomeFile.txt file&quot;);\r\n\t\t\t}\r\n\r\n\t\t\t\/\/----- START THE DOWNLOAD -----\r\n\t\t\tlblState-&gt;Text = &quot;Downloading file...&quot;;\r\n\t\t\tString ^url = &quot;http:\/\/www.somedomain.com\/SomeFile.txt&quot;;\r\n\r\n\t\t\t\/\/ Create an instance of WebClient\r\n\t\t\tWebClient1 = gcnew WebClient();\r\n\r\n\t\t\tWebClient1-&gt;DownloadFileCompleted += gcnew AsyncCompletedEventHandler(this, &amp;frmInstallControlCentre::DownloadFileCompleted);\r\n\t\t\tWebClient1-&gt;DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler(this, &amp;frmInstallControlCentre::DownloadFileProgressCallback);\r\n\r\n\t\t\tWebClient1-&gt;DownloadFileAsync(gcnew Uri(url), TEMP_DOWNLOAD_DIRECTORY + &quot;SomeFile.txt&quot;);\r\n\t\t}\r\n\t\tcatch (Exception ^e)\r\n\t\t{\r\n\t\t\tGlobalObjects::EventLog1-&gt;AddEvent(true, 0x00, Convert::ToString(e));\r\n\t\t}\r\n\t}\r\n\r\n\r\n\t\/\/************************************************\r\n\t\/\/************************************************\r\n\t\/\/********** DOWNLOAD PROGRESS CALLBACK **********\r\n\t\/\/************************************************\r\n\t\/\/************************************************\r\n\tvoid DownloadFileProgressCallback(System::Object^  sender, System::Net::DownloadProgressChangedEventArgs ^ e)\r\n\t{\r\n\t\tif ((e-&gt;ProgressPercentage &gt;= 0) &amp;&amp; (e-&gt;ProgressPercentage &lt;= 100))\r\n\t\t\tDownloadProgressBar-&gt;Value = e-&gt;ProgressPercentage;\r\n\t}\r\n\r\n\r\n\t\/\/************************************************\r\n\t\/\/************************************************\r\n\t\/\/********** DOWNLOAD COMPLETED CALLBACK **********\r\n\t\/\/************************************************\r\n\t\/\/************************************************\r\n\tvoid DownloadFileCompleted(System::Object^  sender, System::ComponentModel::AsyncCompletedEventArgs^ e)\r\n\t{\r\n\t\tif (e-&gt;Cancelled)\r\n\t\t{\r\n\t\t\t\/\/CANCELLED\r\n\r\n\t\t}\r\n\t\telse if (e-&gt;Error != nullptr)\r\n\t\t{\r\n\t\t\t\/\/AN ERROR OCCURED\r\n\r\n\t\t}\r\n\t\telse\r\n\t\t{   \r\n\t\t\t\/\/SUCCESS\r\n\r\n\t\t}\r\n\t}\r\n\r\n\r\n\t\/\/***********************************\r\n\t\/\/***********************************\r\n\t\/\/********** CANCEL BUTTON **********\r\n\t\/\/***********************************\r\n\t\/\/***********************************\r\n\tprivate: System::Void btnCancel_Click(System::Object^  sender, System::EventArgs^  e)\r\n\t{\r\n\r\n\t\t\/\/Cancel the download if it is active\r\n\t\ttry\r\n\t\t{\r\n\t\t\tif (WebClient1 != nullptr)\r\n\t\t\t\tWebClient1-&gt;CancelAsync();\r\n\t\t}\r\n\t\tcatch (Exception ^)\r\n\t\t{\r\n\t\t}\r\n\r\n\t\tthis-&gt;Close();\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; private: WebClient ^WebClient1; \/\/******************************* \/\/******************************* \/\/********** FORM LOAD ********** \/\/******************************* \/\/******************************* private: System::Void frmInstallControlCentre_Load(System::Object^ sender, System::EventArgs^ e) { try { \/\/&#8212;&#8211; CREATE DIRECTORY &#8212;&#8211; if (!Directory::Exists(TEMP_DOWNLOAD_DIRECTORY)) Directory::CreateDirectory(TEMP_DOWNLOAD_DIRECTORY); \/\/&#8212;&#8211; DELETE FILE IF IT ALREADY EXISTS &#8212;&#8211; try { if (File::Exists(TEMP_DOWNLOAD_DIRECTORY + &quot;SomeFile.txt&quot;)) { \/\/If file has read only attribute set clear it so that delete [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-851","post","type-post","status-publish","format-standard","hentry","category-tcp-ip"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=851"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/851\/revisions"}],"predecessor-version":[{"id":1188,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/851\/revisions\/1188"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}