欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

silverlight用webclient大文件上傳的實例代碼

 更新時間:2013年10月14日 14:46:52   作者:  
這篇文章介紹了silverlight用webclient大文件上傳的實例代碼,有需要的朋友可以參考一下
客戶端:
復(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é)果

相關(guān)文章

  • LiteralControl ASP.NET中的另類控件

    LiteralControl ASP.NET中的另類控件

    對于LiteralControl控件的應(yīng)用比較少,今天突然看到了,就弄個明白為好,所以總結(jié)出來,供大家一起學(xué)習(xí)
    2012-01-01
  • ASP.NET?MVC自定義異常過濾器使用案例

    ASP.NET?MVC自定義異常過濾器使用案例

    本文詳細講解了ASP.NET?MVC自定義異常過濾器的使用案例,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下<BR>
    2022-03-03
  • .Net 如何限制用戶登錄的方法

    .Net 如何限制用戶登錄的方法

    這篇文章主要介紹了.Net 如何限制用戶登錄的方法,有需要的朋友可以參考一下
    2013-12-12
  • C#默認以管理員身份運行程序?qū)崿F(xiàn)代碼

    C#默認以管理員身份運行程序?qū)崿F(xiàn)代碼

    權(quán)限不夠,導(dǎo)致無法修改系統(tǒng)時間,于是我以管理員身份運行了一次,結(jié)果測試成功,下面為大家介紹下C#如何默認以管理員身份運行程序
    2014-03-03
  • ASP.NET Global.asax應(yīng)用程序文件簡介

    ASP.NET Global.asax應(yīng)用程序文件簡介

    Global.asax 文件,有時候叫做 ASP.NET 應(yīng)用程序文件,提供了一種在一個中心位置響應(yīng)應(yīng)用程序級或模塊級事件的方法。
    2009-03-03
  • 利用Dom操作字符串一例

    利用Dom操作字符串一例

    利用Dom操作字符串一例,需要的朋友可以參考下。
    2012-01-01
  • ASP.NET?MVC使用Identity增刪改查用戶

    ASP.NET?MVC使用Identity增刪改查用戶

    這篇文章介紹了ASP.NET?MVC使用Identity增刪改查用戶的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • WPF常用控件用法及介紹

    WPF常用控件用法及介紹

    本文詳細講解了WPF常用控件用法及介紹,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • IdentityServer4實現(xiàn).Net Core API接口權(quán)限認證(快速入門)

    IdentityServer4實現(xiàn).Net Core API接口權(quán)限認證(快速入門)

    這篇文章主要介紹了IdentityServer4實現(xiàn).Net Core API接口權(quán)限認證,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Excel、記事本數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實現(xiàn)方法

    Excel、記事本數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實現(xiàn)方法

    將手機號批量導(dǎo)入數(shù)據(jù)庫。思路:先將要導(dǎo)入的文件傳上項目里,然后讀取文件的每行數(shù)據(jù)并插入數(shù)據(jù)庫,操作完后再將上傳的文件刪除
    2013-10-10

最新評論