将IBuffer附加到byte []

时间:2015-11-12 10:24:39

标签: c# windows-runtime windows-store-apps

我有这种方法在Windows 8项目上下载文件

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 个答案:

答案 0 :(得分:1)

The code for reading all bytes for writing it to a file:

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)