02/10/2018, 11:35

[C#] Download file from FTP Server With Progressbar

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: Source code C#: using System; using System.Collections.Generic; using ...

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

Tags: download fileftpserverprogressbar
0