Problems with WebClient.UploadFileAsync

Hi,

Consider the following code:

//--------------------------------------------------------------------------
public Form1 {
   WebClient client = new WebClient();

   client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);

   client.UploadFileCompleted += new UploadFileCompletedEventHandler(client_UploadFileCompleted);

   Uri uri = new Uri(http://example.com/upload.aspx);

   client.UploadFileAsync(uri, @"c:\somefile.bin");
}

void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
   Console.WriteLine(e.ProgressPercentage);
   progressBar1.Value = e.ProgressPercentage;
}

void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
   if(e.Result != null) {
      Console.WriteLine(System.Text.Encoding.ASCII.GetString(e.Result));
   }   
}
//---------------------------------------------------------------------------

Problem i'm experiencing:

When uploading an arbitrary sized file it uploads the file fine. However, the progress is reported in the following manner by e.ProgressPercentage:
000000111111122222233333 ..... 50, then 100, it jumps directly from 50 to 100 no matter what file. Looking at the bytes sent it has sent all the bytes when it has reached 50 percent, and then jumps to 100. The size of the file doesn't matter, the problem remains the same.

The asp.net script accepting the file:

//---------------------------------------------------------------------------
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
   
    foreach(string f in Request.Files.AllKeys) {
        HttpPostedFile file = Request.Files[f];
        file.SaveAs("c:\\inetpub\\wwwroot\\UploadedFiles\\" + file.FileName);
    }   
}

</Script>
<html>
<body>
<p> Upload complete.  </p>
</body>
</html>

//----------------------------------------------------------------------------

The HttpRuntime settings in machine.config is set to accept large files and the timeout is also increased. (Shouldn't matter though since the file is uploaded fine).

Thanks in advance




Answer this question

Problems with WebClient.UploadFileAsync

  • Dan-psg

    Thanks for the reply. I had a feeling that was the case after testing some more.
    In that case, I assume this would be safe considering it never passes 50 when not receiving from server:

    int realProgress = e.ProgressPercentage != 100 e.ProgressPercentage * 2 : e.ProgressPercentage;

    toolStripProgressBar1.Value = realProgress;



  • Artur laksberg

    This by design :-)
    the reason is that when you do an upload the server can also send data as a response to the upload. In general this is true for any HTTP Request. Consider a form uploading a bunch of input and the server sending a whole bunch of data, perhaps an SQL server database report.
    In all these cases we divice the upload and download to 50% and 50%.
    So if the server is not sending any data then it will jump from 50 to 100 directly

  • Adaoud

    Modifying your AsyncDownload codesample at http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=115552&SiteID=1 to do an upload instead gives me the exact same problem.



  • shade_rigal

    Downloading a file with size 3,999,607 bytes, e.ProgressPercentage gives the following:

    0
    0
    0
    0
    1
    2
    3
    4
    4
    5
    6
    7
    8
    9
    9
    10
    11
    12
    13
    13
    14
    15
    16
    17
    18
    18
    19
    20
    21
    22
    22
    23
    24
    25
    26
    27
    27
    28
    29
    30
    31
    31
    32
    33
    34
    35
    36
    36
    37
    38
    39
    40
    40
    41
    42
    43
    44
    45
    45
    46
    47
    48
    49
    49
    50
    51
    52
    53
    54
    54
    55
    56
    57
    58
    58
    59
    60
    61
    62
    63
    63
    64
    65
    66
    67
    68
    68
    69
    70
    71
    72
    72
    73
    74
    75
    76
    77
    77
    78
    79
    80
    81
    81
    82
    83
    84
    85
    86
    86
    87
    88
    89
    90
    90
    91
    92
    93
    94
    95
    95
    96
    98
    98
    98
    100
    100
    ---------------------------------

    Uploading the same file gives the following:
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    3
    3
    3
    3
    3
    3
    3
    3
    3
    3
    4
    4
    4
    4
    4
    4
    4
    4
    4
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    8
    8
    8
    8
    8
    8
    8
    8
    8
    9
    9
    9
    9
    9
    9
    9
    9
    9
    9
    10
    10
    10
    10
    10
    10
    10
    10
    10
    10
    11
    11
    11
    11
    11
    11
    11
    11
    11
    11
    12
    12
    12
    12
    12
    12
    12
    12
    12
    13
    13
    13
    13
    13
    13
    13
    13
    13
    13
    14
    14
    14
    14
    14
    14
    14
    14
    14
    14
    15
    15
    15
    15
    15
    15
    15
    15
    15
    15
    16
    16
    16
    16
    16
    16
    16
    16
    16
    17
    17
    17
    17
    17
    17
    17
    17
    17
    17
    18
    18
    18
    18
    18
    18
    18
    18
    18
    18
    19
    19
    19
    19
    19
    19
    19
    19
    19
    19
    20
    20
    20
    20
    20
    20
    20
    20
    20
    20
    21
    21
    21
    21
    21
    21
    21
    21
    21
    22
    22
    22
    22
    22
    22
    22
    22
    22
    22
    23
    23
    23
    23
    23
    23
    23
    23
    23
    23
    24
    24
    24
    24
    24
    24
    24
    24
    24
    24
    25
    25
    25
    25
    25
    25
    25
    25
    25
    26
    26
    26
    26
    26
    26
    26
    26
    26
    26
    27
    27
    27
    27
    27
    27
    27
    27
    27
    27
    28
    28
    28
    28
    28
    28
    28
    28
    28
    28
    29
    29
    29
    29
    29
    29
    29
    29
    29
    30
    30
    30
    30
    30
    30
    30
    30
    30
    30
    30
    31
    31
    31
    31
    31
    31
    31
    31
    31
    32
    32
    32
    32
    32
    32
    32
    32
    32
    32
    33
    33
    33
    33
    33
    33
    33
    33
    33
    34
    34
    34
    34
    34
    34
    34
    34
    34
    34
    35
    35
    35
    35
    35
    35
    35
    35
    35
    35
    36
    36
    36
    36
    36
    36
    36
    36
    36
    36
    37
    37
    37
    37
    37
    37
    37
    37
    37
    37
    38
    38
    38
    38
    38
    38
    38
    38
    38
    39
    39
    39
    39
    39
    39
    39
    39
    39
    39
    40
    40
    40
    40
    40
    40
    40
    40
    40
    40
    41
    41
    41
    41
    41
    41
    41
    41
    41
    41
    42
    42
    42
    42
    42
    42
    42
    42
    43
    43
    43
    43
    43
    43
    43
    43
    43
    43
    44
    44
    44
    44
    44
    44
    44
    44
    44
    44
    45
    45
    45
    45
    45
    45
    45
    45
    45
    45
    46
    46
    46
    46
    46
    46
    46
    46
    46
    47
    47
    47
    47
    47
    47
    47
    47
    47
    47
    48
    48
    48
    48
    48
    48
    48
    48
    48
    48
    49
    49
    49
    49
    49
    49
    49
    49
    49
    49
    49
    50
    100
    100



  • Dmitry Medvedev

    In that specific case - yes

  • Problems with WebClient.UploadFileAsync