CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011
    Posts
    1

    WebClient Async Upload for multiple servers

    Hi All,
    I'm trying to create an application that can upload the same file to multiple local servers at the same time.

    I'm using this function to upload the file:

    private void UploadFile(string _ip, string _file)
    {
    //Setup the client and Credentials
    WebClient wc = new WebClient();
    wc.Credentials = new NetworkCredential("user", "pass");

    //set the Async routines
    wc.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileDone);
    wc.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgress);

    wc.UploadFileAsync(new Uri("http://" + _ip + DBPath), "POST", _file);
    }


    And When I Press the Start button, this routine runs the "UploadFile" Over each server in the list

    private void btn_Start_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < SiteList.Items.Count; i++)
    {
    UploadFile(SiteList.Items[i].Text, txtURL.Text);
    }
    }


    The Problem is, I Can't seem to find a way to "link" between the routine "UploadProgress" & "UploadFileDone" for each site...

    The ListView Is Like this:


    IP Address | Upload Status
    --------------------------
    10.134.1.1 | Start...
    10.134.1.2 | Uploading 23%
    10.134.1.3 | Done.
    10.134.1.4 | Uploading 71%



    Thanks, for the assistant.

    Liad

  2. #2
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: WebClient Async Upload for multiple servers

    In your call to UploadFileAsync, add an extra parameter (object userToken) that is the ip address or some other unique identifier.

    Code:
    wc.UploadFileAsync(new Uri("http://" + _ip + DBPath), "POST", _file, _ip);
    This Ip will then be passed to your call back method. So, in your methods for UploadFileDone and UploadProgress you will have Event Args available that will contain this ip address in the UserState property of the event args.

    Code:
    void UploadProgress(object sender, UploadProgressChangedEventArgs e)
    {
        string ip = (string)e.UserState;
    }

    Have a look at:
    http://msdn.microsoft.com/en-us/library/ms144227.aspx
    to see a description of userToken.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured