zoukankan      html  css  js  c++  java
  • 实现文件下载c++/cli

    添加net空间,在窗体中添加2个textbox,4个标签label。2个按钮button。设置属性。源码如下:

    namespace 下载 {

     using namespace System;
     using namespace System::ComponentModel;
     using namespace System::Collections;
     using namespace System::Windows::Forms;
     using namespace System::Data;
     using namespace System::Drawing;
     using namespace System::Net;
     /// <summary>
     /// Form1 摘要
     /// </summary>
     public ref class Form1 : public System::Windows::Forms::Form
     {
     public:
      Form1(void)
      {  
       InitializeComponent();
       this->wc=gcnew System::Net::WebClient();
          //this->textBox1->Text="http://ftp.pconline.com.cn/pub/download/201007/QQ2010.zip";//测试地址
       //
       //TODO: 在此处添加构造函数代码
       //
      }

     protected:
      /// <summary>
      /// 清理所有正在使用的资源。
      /// </summary>
      ~Form1()
      {
       if (components)
       {
        delete components;
       }
      }
     private: System::Windows::Forms::Button^  button1;
     protected:
     private: System::Windows::Forms::Button^  button2;
     private: System::Windows::Forms::TextBox^  textBox1;
     private: System::Windows::Forms::Label^  label1;
     private: System::Windows::Forms::Label^  label2;
     private: System::Windows::Forms::Label^  label3;
     private: System::Windows::Forms::Label^  label4;
     private: System::Windows::Forms::ProgressBar^  progressBar1;
     private: System::Net::WebClient ^wc;
     private: System::Windows::Forms::TextBox^  textBox2;
     private: System::Windows::Forms::Label^  label5;
     private:
      /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code
      /// <summary>
      /// 设计器支持所需的方法 - 不要
      /// 使用代码编辑器修改此方法的内容。
      /// </summary>
      void InitializeComponent(void)
      {
       this->button1 = (gcnew System::Windows::Forms::Button());
       this->button2 = (gcnew System::Windows::Forms::Button());
       this->textBox1 = (gcnew System::Windows::Forms::TextBox());
       this->label1 = (gcnew System::Windows::Forms::Label());
       this->label2 = (gcnew System::Windows::Forms::Label());
       this->label3 = (gcnew System::Windows::Forms::Label());
       this->label4 = (gcnew System::Windows::Forms::Label());
       this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
       this->textBox2 = (gcnew System::Windows::Forms::TextBox());
       this->label5 = (gcnew System::Windows::Forms::Label());
       this->SuspendLayout();
       //
       // button1
       //
       this->button1->Location = System::Drawing::Point(27, 160);
       this->button1->Name = L"button1";
       this->button1->Size = System::Drawing::Size(75, 23);
       this->button1->TabIndex = 0;
       this->button1->Text = L"下载";
       this->button1->UseVisualStyleBackColor = true;
       this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
       //
       // button2
       //
       this->button2->Location = System::Drawing::Point(229, 160);
       this->button2->Name = L"button2";
       this->button2->Size = System::Drawing::Size(75, 23);
       this->button2->TabIndex = 1;
       this->button2->Text = L"取消";
       this->button2->UseVisualStyleBackColor = true;
       //
       // textBox1
       //
       this->textBox1->Location = System::Drawing::Point(70, 23);
       this->textBox1->Name = L"textBox1";
       this->textBox1->Size = System::Drawing::Size(333, 21);
       this->textBox1->TabIndex = 2;
       //
       // label1
       //
       this->label1->AutoSize = true;
       this->label1->Location = System::Drawing::Point(23, 26);
       this->label1->Name = L"label1";
       this->label1->Size = System::Drawing::Size(23, 12);
       this->label1->TabIndex = 4;
       this->label1->Text = L"URI";
       //
       // label2
       //
       this->label2->AutoSize = true;
       this->label2->Location = System::Drawing::Point(12, 95);
       this->label2->Name = L"label2";
       this->label2->Size = System::Drawing::Size(29, 12);
       this->label2->TabIndex = 5;
       this->label2->Text = L"进度";
       //
       // label3
       //
       this->label3->AutoSize = true;
       this->label3->Location = System::Drawing::Point(406, 95);
       this->label3->Name = L"label3";
       this->label3->Size = System::Drawing::Size(41, 12);
       this->label3->TabIndex = 6;
       this->label3->Text = L"label3";
       //
       // label4
       //
       this->label4->AutoSize = true;
       this->label4->Location = System::Drawing::Point(77, 125);
       this->label4->Name = L"label4";
       this->label4->Size = System::Drawing::Size(41, 12);
       this->label4->TabIndex = 7;
       this->label4->Text = L"label4";
       //
       // progressBar1
       //
       this->progressBar1->Location = System::Drawing::Point(82, 84);
       this->progressBar1->Name = L"progressBar1";
       this->progressBar1->Size = System::Drawing::Size(294, 23);
       this->progressBar1->TabIndex = 8;
       //
       // textBox2
       //
       this->textBox2->Location = System::Drawing::Point(70, 55);
       this->textBox2->Name = L"textBox2";
       this->textBox2->Size = System::Drawing::Size(333, 21);
       this->textBox2->TabIndex = 9;
       //
       // label5
       //
       this->label5->AutoSize = true;
       this->label5->Location = System::Drawing::Point(12, 58);
       this->label5->Name = L"label5";
       this->label5->Size = System::Drawing::Size(53, 12);
       this->label5->TabIndex = 10;
       this->label5->Text = L"文件名称";
       //
       // Form1
       //
       this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
       this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
       this->ClientSize = System::Drawing::Size(459, 220);
       this->Controls->Add(this->label5);
       this->Controls->Add(this->textBox2);
       this->Controls->Add(this->progressBar1);
       this->Controls->Add(this->label4);
       this->Controls->Add(this->label3);
       this->Controls->Add(this->label2);
       this->Controls->Add(this->label1);
       this->Controls->Add(this->textBox1);
       this->Controls->Add(this->button2);
       this->Controls->Add(this->button1);
       this->Name = L"Form1";
       this->Text = L"Form1";
       this->ResumeLayout(false);
       this->PerformLayout();

      }
    #pragma endregion
     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                          if (wc->IsBusy)
                          {
            wc->CancelAsync();

                          }
           else
           { this->wc->DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler(this,&Form1::wc_WebClient_DownloadProgressChanged);

                this->wc->DownloadFileCompleted +=gcnew AsyncCompletedEventHandler(this,&Form1::wc_WebClient_OnDownloadFileCompleted);
          if (this->textBox1->Text!="")
          {    String ^s=this->textBox1->Text;
                String ^k=s->Remove(0,s->LastIndexOf('/')+1);
             this->textBox2->Text=k;

      this-> wc->DownloadFileAsync(gcnew Uri(this->textBox1->Text),k);
          }
          else
           MessageBox::Show("请输入下载地址");
              }

         }
     private:void wc_WebClient_DownloadProgressChanged(Object ^sender,DownloadProgressChangedEventArgs ^e)
       {

        this->progressBar1->Value=e->ProgressPercentage;
        this->label3->Text=e->ProgressPercentage.ToString()+"%";
        
        this->label4->Text=String::Format("正在下载文件,当前下载{0}/{1}(字节)",e->BytesReceived,e->TotalBytesToReceive);
       }
       

       
    private: void wc_WebClient_OnDownloadFileCompleted(Object ^sender, AsyncCompletedEventArgs ^e)
       {
        
       
        MessageBox::Show("下载完成");
        
       }

    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
        wc->CancelAsync();

       }
    };
    }

    最主要的是事件注册。其他就是对webclient类的了解和应用。

    初学者参考,这里不能上传源码。有值得看的源码全部在csdn了。用户名jinyuttt

  • 相关阅读:
    自己常用网站记录
    css弹性布局指定显示行数多余文字去掉用省略号代替以及弹性布局中css 卡片阴影效果
    微信小程序页面传参被截取问题
    阴影效果 css3 为什么要加 -moz-box-shadow -webkit-box-shadow -o-box-shadow,直接用box-shadow不是都能识别吗?
    css常用清除浮动方式
    什么是微信小程序云开发 它的作用是什么
    JMeter压测“java.net.SocketException: Socket closed”解决方法
    Jmeter压力测试工具安装及使用教程
    OnActionExecuting和OnActionExecuted执行顺序
    C#循环下载多个文件(把多个文件压缩成一个文件可以一次性下载)
  • 原文地址:https://www.cnblogs.com/jinyuttt/p/1775536.html
Copyright © 2011-2022 走看看