views:

51

answers:

1

How can I use HttpWebRequest.BeginGetResponse and get progress changed events for the upload of POST method ?

A portion of the Code I have now is:

HttpWebRequest req = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(uploadUri);

req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + boundary + "";

req.ContentLength = dataBuffer.Length;

req.AllowReadStreamBuffering = false;
req.AllowWriteStreamBuffering = false;

DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
    ProgressText = "Waiting for request stream...";
});

req.BeginGetRequestStream(new AsyncCallback(GotRequestStreamForUpload), new SendPhotoAsyncState(req, dataBuffer, photo));

The call to BeginGetResponse:

request.BeginGetResponse(new AsyncCallback(GotResponseStreamForUpload), new SendPhotoAsyncState(request, null, flickRPhoto));

How can I tell the request that I want to be notified of changes in the stream?

I want to be able to update the UI based on the progress of the photo upload...

The following code doesn't work with WebClient:

       WebClient wc = new WebClient();

        wc.UploadStringCompleted += (s, e) =>
            {
                // What photo is this ?
                FlickRUploadPhoto fup = ((SendPhotoAsyncState)e.UserState).FlickrPhoto2Upload;
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    fup.tCompleted = DateTime.Now;
                    FileUploadComplete(this, new FlickRUploadCompleteEventArgs(fup));
                });

            };

        wc.UploadProgressChanged += (s2,e2) =>
            {

                // Was there an error ?

                // What photo is this ?
                FlickRUploadPhoto fup = ((SendPhotoAsyncState)e2.UserState).FlickrPhoto2Upload;
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        fup.ProgressPercentage = e2.ProgressPercentage;
                        fup.ProgressPercentageString = e2.ProgressPercentage.ToString() + "%";
                        fup.BytesUploaded = e2.BytesSent;
                    });
            };


        string data = dataBuffer.ToString();
        wc.UploadStringAsync(uploadUri, "POST", data , new SendPhotoAsyncState(null, dataBuffer, photo));

This is Silverlight... So I have to use UploadStringAsync ??

A: 

You can't do it with HttpWebRequest, but you can do it with WebClient:

    public void Upload(string targetUri, string filePath)
    {
        WebClient client = new WebClient();
        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
        client.UploadFile(targetUri, filePath);
    }

Or UploadData if you don't have a local file:

public void Upload(string targetUri, byte[] buffer)
    {
        WebClient client = new WebClient();
        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
        client.UploadData(targetUri, buffer);
    }
Joshua
I was under the impression that you can't manipulate the contenttype and headers etc with webclient ?
Greg Foote