silverlight example code uploaded with webclient large file

  • 2020-07-21 07:24:13
  • OfStack

Client:

     /// <summary> 
     ///  Writes data to the stream  
     /// </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"; //  Means to upload a large file 
         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(); 
     }

Server:

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";   // Whether to block upload 
           #region  Save the file 
           //  Initialization directory  
           string dirPath = Path.Combine(ConfigHelper.UploadPath, m.Dir);   //  File save path  
           if (!Directory.Exists(dirPath)) 
               Directory.CreateDirectory(dirPath);
           //  Address of the file  
           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);
               //  Write to the original file  
               Stream sr = Context.Request.InputStream;
               while ((bufLen = sr.Read(buffer, 0, buffer.Length)) > 0) 
                   fs.Write(buffer, 0, bufLen);
               sr.Close(); 
               sr.Dispose();
               //  The thumbnail  
               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  Delete the original file 
           //  Delete the original file 
           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 
       } 

Note: After each stream is saved, read the data in the next block; otherwise, the block flow data before the first block will be overwritten by the block flow data after the first block.
Focus on the results as well as the process

Related articles: