首页 > 扩展 > SilverLight > 正文

silverlight用webclient大文件上传的实例代码

2024-09-06 20:41:35
字体:
来源:转载
供稿:网友
客户端:
代码如下:
     /// <summary>
     /// 写入数据到流中
     /// </summary>
     /// <param name="url"></param>
     /// <param name="callback"></param>
     public async static Task<bool> Write(string url, Stream clientStream)
     {
         if (clientStream.Length > 25*1024*1024)
             url += "&t=1"; // 表示上传大文件
         try
         {
             Up(url, clientStream);
             return true;
         }
         catch { }
         return false;
     }
     public async static Task Up(string url, Stream sourceStream)
     {
         var wc = new WebClient();
         byte[] buffer = new byte[25*1024*1024];
         int bufLen = sourceStream.Read(buffer, 0, buffer.Length);
         if (bufLen < 1)
         {
             sourceStream.Close();
             return;
         }
        wc.WriteStreamClosed += (s, e) =>
         {
             if (sourceStream.CanRead)
                 Up(url, sourceStream);
             else
                 sourceStream.Close();
         };
         var serverStream = await wc.OpenWriteTaskAsync(url, "POST");
         serverStream.Write(buffer, 0, bufLen);
         serverStream.Close();
     }

服务端:
代码如下:
private void Save()
       {
           string data = Context.Request.QueryString["data"].Base64StringDecode("ABC");
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表