如果此問題之前已得到解答,我很遺憾,但沒有任何答案對我有幫助。我有一個事件對我的WebView:掛在較大文件上的HttpClient getAsync
private async void MainWebView_UnviewableContentIdentified(WebView sender, WebViewUnviewableContentIdentifiedEventArgs args)
{
await downloadFromUri(args.Uri).ConfigureAwait(false);
}
我downloadFromUri方法會嘗試下載一個文件,並將其存儲在下載文件夾,最後它會打開文件:
static async Task downloadFromUri(Uri uri)
{
var url = uri;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAppendWithoutValidation(
"Authorization Bearer",
" MYTOKEN"
);
Debug.WriteLine("Attempting GET request");
var response = await client.GetAsync(url);
Debug.WriteLine("Got response.. checking " + response.StatusCode);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine("Okay, we've got a response");
var responseFileName = response.Content.Headers.ContentDisposition.FileName;
Debug.WriteLine("Filename: " + responseFileName);
var ManualFile = await DownloadsFolder.CreateFileAsync(responseFileName, CreationCollisionOption.GenerateUniqueName);
Debug.WriteLine("Creating buffer: ");
var buffer = await client.GetBufferAsync(url);
Debug.WriteLine("Writing buffer.");
await Windows.Storage.FileIO.WriteBufferAsync(ManualFile, buffer);
Debug.WriteLine("Done, opening");
var openOptions = new Windows.System.LauncherOptions();
openOptions.DisplayApplicationPicker = true;
var openFile = await Windows.System.Launcher.LaunchFileAsync(ManualFile, openOptions);
}
else
{
Debug.WriteLine(response.StatusCode.ToString());
}
}
的事情是,此代碼似乎可以在小型PDF或zip文件上正常工作。但是,當涉及到大於10MB的PDF文件時,應用程序只會掛在以下行:
var response = await client.GetAsync(url);
我該怎麼做才能做到這一點?提前感謝!
注意:我使用Windows.Web.Http.HttpClient而不是System.Net.Http.HttpClient。
編輯:我試過跳過client.GetAsync(url)
並直接運行var buffer = await client.GetBufferAsync(url);
。這也會導致死機。
您是否更新了config中的maxRequestLength屬性? 您是否考慮將GetAsync更改爲ReadAsStreamAsync並將Stream寫入文件?該文件不會被下載到內存中,而是直接保存到文件中,我想。 –