欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-29 20:15:58
客户端:复制代码 代码如下:     ///      //...
客户端:
复制代码 代码如下:

     /// <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
       }

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