开发者

Uploading files in .net with WebClient (and silverlight)

开发者 https://www.devze.com 2022-12-30 20:55 出处:网络
I have the code below which I am using to upload files to my ashx page. It works great, although I cant seem to find a proper way of getting how much it has transferred.

I have the code below which I am using to upload files to my ashx page. It works great, although I cant seem to find a proper way of getting how much it has transferred.

The calling code:

WebClient wc = new WebClient();
wc.OpenWriteCompleted += (s2, e2) =>
{
   PushData(e2.Result, offset);
   e2.Result.Close();
};
wc.OpenWriteAsync(ub.Uri);

The push data code:

private void PushData(Stream output, long offset)
{
    byte[] buffer = new byte[4096];
    int bytesRead;
    bytesRead = theFileStream.Read(buffer, 0, buffer.Length);
    if (bytesRead != 0)
    {
        output.Write(buffer, 0, bytesRead);

        totalBytesDone += bytesRead;
        FireUpdateEvent(bytesRead);
    }
}

The above code is slightly different to my actual code, for brevity sake. Now, I had presumed that when it gets to output.Write(buffer,0,bytesRead); that that was the point where it sent the actual data and it would lock up and only goto the next line once its finished writing that section. But it goes on to totalBytesDone += bytesRead; before its written anything to the开发者_StackOverflow中文版 server. I presume the reason is that its doing the writing in a separate thread in the background (or I'm actually looking at the wrong section of code and it writes somewhere else) - but for my totalBytesDone code to work I want it to lock up until its finished sending (I can put this all in a seperate thread later).

I've downloaded tons of examples for doing this and they either dont work properly with my ashx file handler (I cant change it) or they use a WebClient method that just reports on 50% progress.


Take a look at this answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号