Tạo Form Download bằng C# | Form tải về

Đăng bởi : 10/26/2013


Form Download
Demo: https://www.dropbox.com/s/2gdx3xb93fqbtzv/DownLoadBangC%23-thuvienwinform.rar

Để thực hiện công việc này ta phải sử dụng đến thư viện System.Net; -> hỗ trợ toàn bộ các bạn .NET
Cú pháp như sau:


WebClient Wc = new WebClient();//Khai báo biến webclient để thực hiện tải về
Uri FileUrl = new Uri(txtUrl.Text.Trim());//Uri để tạo đầu vào cho Wc tải về, Trim để xóa kí tự rỗng ở 2 đầu
Wc.DownloadFileAsync(FileUrl, txtSaveFile.Text);//Bắt đầu tải về Cú pháp: TênBiếnWebclient.DownloadFileAsync(Uri chứa đường dẫn tải về, Địa điểm lưu file)

Code chương trình:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;//thư viện cần thiết để tạo download

namespace DownLoad
{
    public partial class Form1 : Form
    {
        string FileSave;//biến chứa đường dẫn lưu files
        WebClient Wc = new WebClient();//Khai báo biến webclient để thực hiện tải về
        public Form1()
        {
            InitializeComponent();
        }

        private void Save_File()//chọn đường dẫn lưu file
        {
            SaveFileDialog saveDial = new SaveFileDialog();//Hộp thoại tạo file lưu trữ
            saveDial.Title = "Lưu file";//tên hộp thoại
            saveDial.Filter = "Tất cả|*.*";//Định dạnh file lưu
            if (saveDial.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)//Nếu không ấn hủy thì sẽ lấy đường dẫn lưu file
                FileSave = saveDial.FileName;//Lấy đường dẫn để lưu file
            txtSaveFile.Text = FileSave;//Xuất đường dẫn ra textbox
        }

        private void btBrower_Click(object sender, EventArgs e)//Sự kiện ấn nút chọn đường dẫn lưu file
        {
            Save_File();
        }

        private void btStartDownLoad_Click(object sender, EventArgs e)
        {
            Wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Wc_DownloadProgressChange);//Hiển thị phần trăm tải về bằng progressBar
            Uri FileUrl = new Uri(txtUrl.Text.Trim());//Uri để tạo đầu vào cho Wc tải về, Trim để xóa kí tự rỗng ở 2 đầu
            if (txtSaveFile.Text == "" || txtSaveFile.Text == null)//Nếu chưa chọn đường dẫn để lưu thì phải chọn xong mới tải
                Save_File();
            else//Nếu đã chọn đường dẫn lưu file thì bắt đầu tải
                Wc.DownloadFileAsync(FileUrl, txtSaveFile.Text);//Bắt đầu tải về Cú pháp: TênBiếnWebclient.DownloadFileAsync(Uri chứa đường dẫn tải về, Địa điểm lưu file)
        }

        void Wc_DownloadProgressChange(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;//Gán phần trăm tải về vào ProgressBar
            lbPercent.Text = progressBar1.Value.ToString() + "%";
        }
    }
}
ấn F5 -> điền dữ liệu vào form và ấn bắt đầu:
VD:
+ Đường dẫn: https://lh5.googleusercontent.com/-4KAQJWN3Zl8/Ulo8Bj6G3EI/AAAAAAAAACc/uwklKtW1GHA/w499-h300-no/Microsoft-Visual-Studio-2012.png
+ Lưu vào: D:\a.png



{ 8 comments }

  1. anh oi. nhung lam sao de minh co the tai ve vs ten goc cua file. em thay minh phai tu dinh nghia ten file khong ak?

    ReplyDelete
    Replies
    1. Dạ có thể tách lấy tên file từ Uri ạ. Tên file chính là phần cuối của uri ạ

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. sau khi vào đường dẫn lưu file. không chọn save được bạn à

    ReplyDelete
  4. thầy cho em hỏi chỗ hiển thi phần trăm download dùng công cụ gì vậy ạ

    ReplyDelete
  5. thầy cho em hỏi chỗ hiển thi phần trăm download dùng công cụ gì vậy ạ

    ReplyDelete
  6. a ơi sao e code nó chạy bth! mà khi đến 100% rồi vẫn chả thấy file tải về đâu ạ?

    ReplyDelete

Nhận ngay 100$ cho VPS

Mua hàng ủng hộ page

Ủng hộ page

Nhãn

Code (45) Team Foundation Server (17) Database (14) News (14) product (13) toolbox (10) Linq (9) SoftDesign (8) XNA (6) API (5) Project (5) item (4)

- Bản quyền thuộc về Thư Viện WinForm - Giao diện: Metrominimalist - Thiết kế: Johanes Djogan -