大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

如何编写silverlight用webclient大文件上传的代码-创新互联

这篇文章主要讲解了“如何编写silverlight用webclient大文件上传的代码”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何编写silverlight用webclient大文件上传的代码”吧!

我们提供的服务有:成都网站设计、成都网站建设、微信公众号开发、网站优化、网站认证、温泉ssl等。为上1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的温泉网站制作公司

客户端:


复制代码 代码如下:

     ///


     /// 写入数据到流中
     ///

     ///
     ///
     public async static Task 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(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
       }



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

感谢各位的阅读,以上就是“如何编写silverlight用webclient大文件上传的代码”的内容了,经过本文的学习后,相信大家对如何编写silverlight用webclient大文件上传的代码这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!


当前标题:如何编写silverlight用webclient大文件上传的代码-创新互联
本文来源:http://dzwzjz.com/article/didsjc.html
在线咨询
服务热线
服务热线:028-86922220
TOP