通过使用http请求检索的流保存大文件

编程入门 行业动态 更新时间:2024-10-26 22:26:17
本文介绍了通过使用http请求检索的流保存大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在保存http请求收到的文件时遇到问题。如果我在byte []中复制流应用程序内存不足,所以我尝试使用inputstream / outputstream ...但输入流的canseek属性是等于假所以我不能使用inputstream.position和inputstream.lenght ...我该如何解决这个问题?

Hi, i have a problem with saving a file received by an http request. If i copy the stream in a byte[] the application run out of memory, so i tried to do with inputstream/outputstream...but the canseek property of the inputstream is equal to false and so i cannot use inputstream.position and inputstream.lenght...how can i solve this?

这里代码:

//i receive a stream by an http request var stream = await response.Content.ReadAsInputStreamAsync(); Stream inputStream = stream.AsStreamForRead(); //create a video file StorageFolder folder = KnownFolders.VideosLibrary; StorageFile video = await folder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName); System.IO.Stream outstream = await video.OpenStreamForWriteAsync(); const int bufferSize = 4096; while (inputStream.Position < inputStream.Length)//inputStream has canSeek= false so i receive an exception { byte[] buffer = new byte[bufferSize]; int amountRead = inputStream.Read(buffer, 0, bufferSize); outstream.Write(buffer, 0, amountRead); } outstream.Flush();

推荐答案

我建议您使用DataReader和DataWriter来读取和写入数据。

I suggest you using the DataReader and DataWriter to read and write the steam.

using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.Web.Http; using System.Threading; using System; using Windows.Storage.Streams; namespace CommonLab { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { private HttpClient httpClient; private CancellationTokenSource cts; private const uint BUFFER_SIZE = 64; public MainPage() { this.InitializeComponent(); cts = new CancellationTokenSource(); httpClient = new HttpClient(); } private async void Button_Click(object sender, RoutedEventArgs e) { Uri uri = new Uri("www.google"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri); HttpResponseMessage response = await httpClient.SendRequestAsync( request, HttpCompletionOption.ResponseHeadersRead).AsTask(cts.Token); var stream = await response.Content.ReadAsInputStreamAsync(); //in your case, reapleace the memory stream by file stream var outputStream = new InMemoryRandomAccessStream(); using (var dataWriter = new DataWriter(outputStream.GetOutputStreamAt(0))) { using (var dataReader = new DataReader(stream)) { while (true) { var bytesRead = await dataReader.LoadAsync(BUFFER_SIZE); var buffer = dataReader.ReadBuffer(Math.Min(bytesRead, BUFFER_SIZE)); dataWriter.WriteBuffer(buffer); if (bytesRead < BUFFER_SIZE) { break; } await dataWriter.StoreAsync(); await dataWriter.FlushAsync(); } dataWriter.DetachStream(); // verify the output var reader = new DataReader(outputStream.GetInputStreamAt(0)); var size = await reader.LoadAsync((uint)outputStream.Size); var html = reader.ReadString(size); } } } } }

问候,

Jeffrey

更多推荐

通过使用http请求检索的流保存大文件

本文发布于:2023-11-25 17:36:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大文件   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!