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

    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
    -------------------------- | Start... | Uploading 23% | Done. | Uploading 71%

    Thanks, for the assistant.


  2. #2
    Join Date
    Mar 2011

    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.

    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.

    void UploadProgress(object sender, UploadProgressChangedEventArgs e)
        string ip = (string)e.UserState;

    Have a look at:
    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)