主頁(yè) > 知識(shí)庫(kù) > silverlight用webclient大文件上傳的實(shí)例代碼

silverlight用webclient大文件上傳的實(shí)例代碼

熱門(mén)標(biāo)簽:巫師3為什么地圖標(biāo)注的財(cái)寶沒(méi)有 寧波自動(dòng)外呼系統(tǒng)代理 手機(jī)地圖標(biāo)注如何刪除 十堰正規(guī)電銷(xiāo)機(jī)器人系統(tǒng) 世紀(jì)佳緣地圖標(biāo)注怎么去掉 怎么給超市做地圖標(biāo)注入駐店 外呼系統(tǒng)費(fèi)用一年 辦理400電話(huà)證件 外呼系統(tǒng)代理品牌
客戶(hù)端:
復(fù)制代碼 代碼如下:

     /// summary>
     /// 寫(xiě)入數(shù)據(jù)到流中
     /// /summary>
     /// param name="url">/param>
     /// param name="callback">/param>
     public async static Taskbool> 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();
     }

服務(wù)端:
復(fù)制代碼 代碼如下:

private void Save()
       {
           string data = Context.Request.QueryString["data"].Base64StringDecode("ABC");
           if (data.IsNullOrEmpty())
               return;
           var m = JsonConvert.DeserializeObjectFileUploadModel>(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);
               // 寫(xiě)入原文件
               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
       }

分塊上傳注意點(diǎn):每塊流保存完以后再去讀取下以塊的數(shù)據(jù),不然會(huì)多塊一起過(guò)來(lái)會(huì)前面的塊流數(shù)據(jù)會(huì)被后面的塊流數(shù)據(jù)覆蓋;
注重過(guò)程的同時(shí)注重結(jié)果
您可能感興趣的文章:
  • 用WebClient.UploadData方法上載文件數(shù)據(jù)的方法
  • Silverlight中同步調(diào)用WebClient的解決辦法,是同步!
  • C#中在WebClient中使用post發(fā)送數(shù)據(jù)實(shí)現(xiàn)方法
  • 基于WebClient實(shí)現(xiàn)Http協(xié)議的Post與Get對(duì)網(wǎng)站進(jìn)行模擬登陸和瀏覽實(shí)例
  • C# WebClient類(lèi)用法實(shí)例
  • C# webclient中文亂碼問(wèn)題解決方法

標(biāo)簽:景德鎮(zhèn) 通遼 山西 泰州 天門(mén) 嘉興 牡丹江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《silverlight用webclient大文件上傳的實(shí)例代碼》,本文關(guān)鍵詞  silverlight,用,webclient,大,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《silverlight用webclient大文件上傳的實(shí)例代碼》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于silverlight用webclient大文件上傳的實(shí)例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章