Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Uploading with WebClient

Uploading with WebClient

Scheduled Pinned Locked Moved C#
sysadminhelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Cavanagh
    wrote on last edited by
    #1

    Hi. 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 server. I presume the reason is that its doing the writing in a seperate thread in the background(or im 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). Ive 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. Please help, this is urgent. Thanks :)

    Strive to be humble enough to take advice, and confident enough to do something about it.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups