Win Phone 8.1:下载大文件时出现OutOfMemory异常(30 MB)

时间:2015-03-25 13:24:15

标签: windows-phone windows-phone-8.1

我正在创建一个通用的Windows Phone 8.1应用程序。我正在尝试下载该文件并将其查看到启动器中。我适用于小于15 MB文件的小文件。但是当文件大小超过15 MB时,我得到了内存不足的异常。

 async private Task<object> GetMailAttachments(string attachNotify)
    {
        try
        {
            cmdBarMailItem.IsEnabled = false;

            if (await Device.IsNetworkAvailable())
            {
                cts = new CancellationTokenSource();
                // Ignore SSL Certificate which is untrusted,expired and has invalid hostname.
                var filter = new HttpBaseProtocolFilter() { AllowUI = false };
                filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Untrusted);
                filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Expired);
                filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.InvalidName);

                // Start calling the soap service #userGetAttachmentByIndex
                using (var client = new System.Net.Http.HttpClient(new WinRtHttpClientHandler(filter)))
                {
                    //Prepare parameters which is to be post via soap envelope.
                    List<KeyValuePair<string, string>> parameter = new List<KeyValuePair<string, string>>();

                    parameter.Add(new KeyValuePair<string, string>("sessionId", GlobalInfo.SessionID));
                    parameter.Add(new KeyValuePair<string, string>("attachmentIndex", attachNotify.Split('|')[1].ToString()));

                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
                    client.DefaultRequestHeaders.Add("SOAPAction", "userGetAttachmentByIndex");

                    var postContent = new StringContent(StringHelper.ConstructSoapRequest(parameter, "userGetAttachmentByIndex"), Encoding.UTF8, "text/xml");

                    // Getting response from soap service
                    var response = await client.PostAsync(new Uri(AppEnv.ServiceEndPoint), postContent, cts.Token);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {

                        string soapResponse = await response.Content.ReadAsStringAsync();
                        var soap = XDocument.Parse(soapResponse);
                        XNamespace ns = "http://service.webservice.cryoserver.ci";
                        var base64BinaryStr = soap.Descendants(ns + "userGetAttachmentByIndexResponse").First().Descendants(ns + "return").First().Descendants(ns + "attachmentType").First().Descendants(ns + "binaryData").First().Descendants(ns + "base64Binary").First().Value;

                        await saveStringToLocalFile(base64BinaryStr);

                        var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("myTest.pdf");

                        bool x = await Windows.System.Launcher.LaunchFileAsync(file);

                        return x;
                    }
                }
            }
            cmdBarMailItem.IsEnabled = true;
        }
        catch (TaskCanceledException)
        {
            PopupRetrieve.IsOpen = false;
            ProgressBar.IsVisible = false;
            cmdBarMailItem.IsEnabled = true;
        }
        catch(Exception ex)
        {
            cmdBarMailItem.IsEnabled = true;
            ProgressBar.IsVisible = false;
            MessageBox.Show(AlertType.Connectivity);
        }
        return null;
    }

async Task saveStringToLocalFile(string content)
    {
        try
        {
            // saves the string 'content' to a file 'filename' in the app's local storage folder
            // byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(content.ToCharArray());
            byte[] byteArray = Convert.FromBase64String(content);

            // create a file with the given filename in the local folder; replace any existing file with the same name
            StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("myTest.pdf", CreationCollisionOption.ReplaceExisting);

            // write the char array created from the content string into the file
            using (var stream = await file.OpenStreamForWriteAsync())
            {
                stream.Write(byteArray, 0, byteArray.Length);
                stream.Flush();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我在执行

行时遇到异常
string soapResponse = await response.Content.ReadAsStringAsync();

任何人都知道为什么会发生异常?什么可能解决方案来解决它。

任何帮助都是有益的。 :)

0 个答案:

没有答案
相关问题