如何編寫silverlight用webclient大文件上傳的代碼-創(chuàng)新互聯(lián)

這篇文章主要講解了“如何編寫silverlight用webclient大文件上傳的代碼”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何編寫silverlight用webclient大文件上傳的代碼”吧!

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、溫泉ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的溫泉網(wǎng)站制作公司

客戶端:


復(fù)制代碼 代碼如下:

     /// <summary>
     /// 寫入數(shù)據(jù)到流中
     /// </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();
     }



服務(wù)端:


復(fù)制代碼 代碼如下:

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
       }



分塊上傳注意點:每塊流保存完以后再去讀取下以塊的數(shù)據(jù),不然會多塊一起過來會前面的塊流數(shù)據(jù)會被后面的塊流數(shù)據(jù)覆蓋;
注重過程的同時注重結(jié)果

感謝各位的閱讀,以上就是“如何編寫silverlight用webclient大文件上傳的代碼”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何編寫silverlight用webclient大文件上傳的代碼這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

標(biāo)題名稱:如何編寫silverlight用webclient大文件上傳的代碼-創(chuàng)新互聯(lián)
鏈接分享:http://bm7419.com/article22/didsjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)企業(yè)建站、移動網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、域名注冊、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)