Server response / callback after loading Silverlight file

Well, first of all, unicorn avatars are fun. Seriously, I thought my account was compromised. Happy April Fool.

Now I upload files through Silverlight to the server. What is the best way to notify Silverlight that files have been downloaded? It is even possible to throw other information, such as success / failure, etc.

I followed a simple tutorial here for my file upload logic

+3
source share
2 answers

First of all, rewrite 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 as follows: -

   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();
    }
+2
source

WebClient Silverlight.

UploadStringAsync :

byte[] bytes = this.Encoding.GetBytes(data);

, base64 ( ):

  public class WebClientUploaderBase64Encoding : System.Text.Encoding
  {
    public override int GetMaxCharCount(int byteCount) {throw new NotImplementedException();}

    public override int GetMaxByteCount(int charCount){throw new NotImplementedException();}

    public override int GetCharCount(byte[] bytes, int index, int count){throw new NotImplementedException();}

    public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex){throw new NotImplementedException();}

    public override int GetByteCount(char[] chars, int index, int count)
    {
      var data = System.Convert.FromBase64CharArray(chars, index, count);
      return data.Length;
    }

    public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
    {
      var data = System.Convert.FromBase64CharArray(chars, charIndex, charCount);

      for (int index = 0; index < data.Length; ++index)
        bytes[byteIndex + index] = data[index];
      return data.Length;
    }

    public override string GetString(byte[] bytes, int start, int length)
    {
      return System.Convert.ToBase64String(bytes, start, length);
    }
  }

:

private async Task UploadFile(byte[] data)
{
  var client = new WebClient() {Encoding = new WebClientUploaderBase64Encoding()};
  string base64EncodedData = client.Encoding.GetString(data, 0, data.Length);
  var base64EncodedResult = await client.UploadStringTaskAsync("http://api/query", base64EncodedData);
  var resultBytes = client.Encoding.GetBytes(base64EncodedResult);
  var json = Encoding.UTF8.GetString(resultBytes);
}
+1

Source: https://habr.com/ru/post/1739468/


All Articles