0

I've posted this question over in the ASP.Net forums too: http://forums.asp.net/t/2014412.aspx?FTP+Upload+with+Progress

My situation is that I need to save uploads via FTP (with credentials) to another server (ie, not the web server). I've been able to do this, but I cannot fathom how to show progress to the GUI.

I was advised to use a background worker, but this doesn't update the GUI. Here is my attempt:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Net;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    BackgroundWorker backgroundWorker1 = new BackgroundWorker();


    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

        var fileName = @"c:\myfile.zip";
        var ftpWebRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://servername/myfile.zip"));
        ftpWebRequest.Credentials = new NetworkCredential("myusername", "mypassword");
        ftpWebRequest.KeepAlive = false;
        ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpWebRequest.UseBinary = true;
        FileInfo fileInf = new FileInfo(fileName);

        //Notify the server about the size of the uploaded file
        ftpWebRequest.ContentLength = fileInf.Length;


        ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
        using (var inputStream = File.OpenRead(fileName))
        using (var outputStream = ftpWebRequest.GetRequestStream())
        {
            var buffer = new byte[1024 * 1024];
            int totalReadBytesCount = 0;
            int readBytesCount;
            while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                outputStream.Write(buffer, 0, readBytesCount);
                totalReadBytesCount += readBytesCount;
                var progress = totalReadBytesCount * 100.0 / inputStream.Length;
                backgroundWorker1.ReportProgress((int)progress, totalReadBytesCount);
            }
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {       
            progressBar.Value = e.ProgressPercentage;
            percentTB.Text = e.ProgressPercentage + "%";       
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
        backgroundWorker1.RunWorkerAsync();
    }

    private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // log when the worker is completed.
    }
}

Any advice is appreciated.

Captain_Planet
  • 1,228
  • 1
  • 12
  • 28
  • have you checked this particular `SO` Previous posting http://stackoverflow.com/questions/4584789/connecting-ftp-server-with-credentials – MethodMan Oct 28 '14 at 16:12
  • Your server side code is not going to update the browser once you have returned the response. If you want to update the browser whilst the upload is taking place from the server to another FTP server then you will need some kind of web service and update the browser using JS. – Ben Robinson Oct 28 '14 at 16:14

0 Answers0