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

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

问题描述

好吧,首先,独角兽的化身是可笑的。说真的,我以为我的帐户被盗用了。愚人节快乐。

现在我通过Silverlight将文件上传到服务器。通知Silverlight文件已上传的最佳方式是什么?也许甚至折腾其他信息,如成功/失败等。

我跟着一个简单的教程这里为我的文件上传逻辑 解决方案

首先重写UploadFile函数,如下所示: -

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

WebClient c = new WebClient();
c.OpenWriteCompleted + =(sender,e)=>
{
尝试
{
PushData(data,e.Result);
e.Result.Close();
data.Close(); //阻止直到上传完成
callback(null);

catch(Exception er)
{
callback(er);
}
};
c.OpenWriteAsync(ub.Uri);





$ b现在你可以像这样使用这个函数:

 流数据= new fi.OpenRead(); 
尝试
{
FileUpload(fi.Name,data,(err)=>
{
//注意,如果你想摆弄UI调度器
if(err == null)
{
//成功
}
else
{
//失败做某事与错误显然为什么
}
});


catch
{
data.Dispose();
}


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

解决方案

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天全站免登陆