• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
客户端:
复制代码 代码如下:

     /// <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");
           if (data.IsNullOrEmpty())
               return;
           var m = JsonConvert.DeserializeObject<FileUploadModel>(data);
           if (m == null)
               return;
           var isSplitBlock = Context.Request.QueryString["t"]=="1";   //是否分块上传
           #region 保存文件
           // 初始化目录
           string dirPath = Path.Combine(ConfigHelper.UploadPath, m.Dir);   // 文件保存路径
           if (!Directory.Exists(dirPath))
               Directory.CreateDirectory(dirPath);
           // 文件地址
           string filePath = Path.Combine(dirPath, m.FileName);
           if (!isSplitBlock)
           {
               if (File.Exists(filePath))
                   File.Delete(filePath);
           }
           int bufLen = 0;
           byte[] buffer = new byte[4096];
           using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
           {
               fs.Seek(0, SeekOrigin.End);
               // 写入原文件
               Stream sr = Context.Request.InputStream;
               while ((bufLen = sr.Read(buffer, 0, buffer.Length)) > 0)
                   fs.Write(buffer, 0, bufLen);
               sr.Close();
               sr.Dispose();
               // 缩略图
               try
               {
                   if (!m.NeedThumbnail)
                       return;
                   dirPath = Path.Combine(dirPath, "Small");
                   if (!Directory.Exists(dirPath))
                       Directory.CreateDirectory(dirPath);
                   filePath = Path.Combine(dirPath, m.FileName);
                   if (File.Exists(filePath))
                       File.Delete(filePath);
                   using (var pic = GetThumbnail(fs, 300, 300))
                   {
                       pic.Save(filePath);
                   }
               }
               catch { }
           }
           #endregion
           #region 删除原文件
           // 删除原文件
           if (m.OldFilePath.IsNullOrEmpty())
           {
               return;
           }
           try
           {
               filePath = Path.Combine(ConfigHelper.UploadPath, m.OldFilePath);
               if (File.Exists(filePath))
                   File.Delete(filePath);
               if (m.NeedThumbnail)
               {
                   filePath = Path.Combine(ConfigHelper.UploadPath, m.OldThumbnailImagePath);
                   if (File.Exists(filePath))
                       File.Delete(filePath);
               }
           }
           catch (Exception ex)
           {
           }
           #endregion
       }

分块上传注意点:每块流保存完以后再去读取下以块的数据,不然会多块一起过来会前面的块流数据会被后面的块流数据覆盖;
注重过程的同时注重结果

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Queryable.Union 方法实现json格式的字符串合并的具体实例发布时间:2022-02-05
下一篇:
TreeView无刷新获取text及value实现代码发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap