views:

150

answers:

1

Ok first of all, the unicorn avatars are hilarious. Seriously, I thought my account was compromised. Happy April Fool's day.

Now I'm uploading files via Silverlight to the server. What is the best way to notify Silverlight that the files have been uploaded? Perhaps even toss other information back such as success/failure etc.

I followed a simple tutorial here for my file upload logic

+1  A: 

First of all re-write the UploadFile function as follows:-

    private void UploadFile(string fileName, Stream data, Action<Exception> callback)
    {
        UriBuilder ub = new UriBuilder("http://localhost:3840/receiver.ashx");
        ub.Query = string.Format("filename={0}", fileName);

        WebClient c = new WebClient();
        c.OpenWriteCompleted += (sender, e) =>
        {
            try
            {
              PushData(data, e.Result);
              e.Result.Close();
              data.Close();  // This blocks until the upload completes
              callback(null);
            }
            catch (Exception er)
            {
              callback(er);
            }
        };
        c.OpenWriteAsync(ub.Uri);
    }

Now you can use this function like this:-

   Stream data = new fi.OpenRead();
   try
   {        
       FileUpload(fi.Name, data, (err) => 
        {
           // Note if you want to fiddle with the UI use dispatcher Invoke here.
           if (err == null)
           {
              // Success
           }
           else
           {
              // Fail do something with the err to disply why
           }
        });

    }
    catch
    {
        data.Dispose();
    }
AnthonyWJones
What if I wanted to talk back to Silverlight the "Id" of the uploaded file?
Matt
@Matt: Ah now that is a different story, you are wanting to also get data back from the server. For reasons I have not yet found convincing arguments for WebClient doesn't support that scenario directly. A strange design choice IMHO but what I know. However it may be possible to sub-class WebClient to add it.
AnthonyWJones
@AnthonyWJones: Well I don't plan to do that much effort. Looks like I'll just implement plan B. I'll save my Silverlight objects first with WCF, and then upload files by passing the Id through the querystring. Then I can make a relation between the two.
Matt
You've got a () in your try
Chris S
@Chris: thanks, fixed.
AnthonyWJones
@AnthonyWJones: how can I use the second code block above? Do I create a new callback function and put that code there? what about the FileUpload?
VoodooChild
@AnthonyWJones: Nevermind I got it now! However if you can, could you please teach me how I can use dispatcher Invoke when there is an error?
VoodooChild
@AnthonyWJones: Nevermind I got it now, whatever exception is caught in UploadFile can be shown like `Dispatcher.BeginInvoke(new Action(() => ShowError("fail...." + err.Message)));` where the `ShowError` method has a messagebox in my case..
VoodooChild
+1 - thanks, the more I am looking into it the more I am finding out. Here is my question worth 100 points (stackoverflow.com/questions/3720774/…) – VoodooChild 1 hour ago
VoodooChild