NEWS

[C#] Download file from FTP Server With Progressbar

[C#] Download file from FTP Server With Progressbar
Đăng bởi: Thảo Meo - Lượt xem: 11998 08:10:58, 12/03/2018C#   In bài viết

Bài viết hôm nay, mình sẽ tiếp tục hướng dẫn các bạn cách download một file từ FTP server có Progress bar bằng C#.

Dưới dây là giao diện demo ứng dụng:

download file from ftp server c#

Source code C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DownloadFTPWithProgress
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task.Run(() => Download());
        }

        private void Download()
        {
            try
            {
                const string url = "ftp://192.168.0.3/data/testdownload.rara";
                NetworkCredential credentials = new NetworkCredential("nguyenthao", "123456");
                label1.BeginInvoke(new Action(() =>
                {
                    label1.Text = "Đang download file ...";
                }));


                WebRequest sizeRequest = WebRequest.Create(url);
                sizeRequest.Credentials = credentials;
                sizeRequest.Method = WebRequestMethods.Ftp.GetFileSize;
                int size = (int)sizeRequest.GetResponse().ContentLength;

                progressBar1.Invoke(
                    (MethodInvoker)(() => progressBar1.Maximum = size));

                WebRequest request = WebRequest.Create(url);
                request.Credentials = credentials;
                request.Method = WebRequestMethods.Ftp.DownloadFile;

                using (Stream ftpStream = request.GetResponse().GetResponseStream())
                using (Stream fileStream = File.Create(@"D:	estdownload.rara"))
                {
                    byte[] buffer = new byte[10240];
                    int read;
                    while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fileStream.Write(buffer, 0, read);
                        int position = (int)fileStream.Position;

                        progressBar1.BeginInvoke(new Action(() =>
                        {
                            progressBar1.Value = position;
                        }));
                        if (position == size)
                        {
                            label1.BeginInvoke(new Action(() =>
                            {
                                label1.Text = "Download finish!";
                            }));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }


    }
}

HAVE FUN :)

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Download file from FTP Server With Progressbar
Đăng bởi: Thảo Meo - Lượt xem: 11998 08:10:58, 12/03/2018C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.