【问题标题】:Append IBuffer to byte[]将 IBuffer 附加到 byte[]
【发布时间】:2015-11-12 10:24:38
【问题描述】:

我有这种方法可以在 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);
            }

我想将我在缓冲区中获得的字节附加到数据变量中,以便最后我将所有字节都保存在数据中

我该怎么做?

【问题讨论】:

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


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-29
      • 2013-05-31
      • 1970-01-01
      • 2012-01-02
      • 2016-02-26
      • 2011-03-25
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多