2015-11-12 47 views
0

我有這樣的方法來下載一個Windows中的文件8項目追加IBuffer爲byte []

try{ 
byte[] data; 
... 

    Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(); 

        client.DefaultRequestHeaders.TryAppendWithoutValidation(
         "Authorization", 
         "Bearer " + App.Current.Resources["token"] as string); 

        Uri uri = new Uri(Constants.baseAddress + "meeting/points/attachments/download?meetingId=" + meetingId + "&pointId=" + pointId + "&attachmentId=" + attachmentId); 


         Windows.Web.Http.HttpResponseMessage response = await client.GetAsync(uri, Windows.Web.Http.HttpCompletionOption.ResponseHeadersRead); 

         IInputStream inputStream = await response.Content.ReadAsInputStreamAsync(); 

         ulong totalBytesRead = 0; 
         while (true) 
         { 
          // Read from the web. 
          IBuffer buffer = new Windows.Storage.Streams.Buffer(1024); 

          buffer = await inputStream.ReadAsync(
           buffer, 
           buffer.Capacity, 
           InputStreamOptions.None); 

          if (buffer.Length == 0) 
          { 
           // There is nothing else to read. 
           break; 
          } 

          // Report progress. 
          totalBytesRead += buffer.Length; 
          System.Diagnostics.Debug.WriteLine("Bytes read: {0}", totalBytesRead); 

          // Write to file. 
         } 

         inputStream.Dispose(); 

       } 
      } 
      catch (Exception e) 
      { 
       Debug.WriteLine(e.Message); 
      } 

我想追加我在緩衝區中的數據變量獲得字節,所以,在結束我已保存的所有字節數據

我該怎麼做?

回答

1

讀取所有字節爲它編寫到一個文件中的代碼:

byte[] data; 
IInputStream inputStream = await response.Content.ReadAsInputStreamAsync(); 
data = new byte[inputStream .Length]; 
inputStream.Read(data, 0, (int)inputStream.Length); 

//Save the file here (data)