Silverlight 文件上传后的服务器响应/回调 [英] Server response / callback after Silverlight file upload

查看:20
本文介绍了Silverlight 文件上传后的服务器响应/回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,首先,独角兽化身很搞笑.说真的,我以为我的帐户被盗用了.愚人节快乐.

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

现在我通过 Silverlight 将文件上传到服务器.通知 Silverlight 文件已上传的最佳方式是什么?甚至可能会返回其他信息,例如成功/失败等.

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

推荐答案

首先重写UploadFile函数如下:-

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

这篇关于Silverlight 文件上传后的服务器响应/回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆