Exemplo n.º 1
0
        public async Task<bool> DownloadAsyncForce(IProgress<int> progress)
        {
            bool fSaved = false;

            try
            {
                // Download from the inter-tubes
                HttpWebRequest hwreq = WebRequest.Create(m_u) as HttpWebRequest;
                HttpWebResponse hwresp = (await hwreq.GetResponseAsync()) as HttpWebResponse;

                if (hwresp.ResponseUri.AbsoluteUri == m_u.AbsoluteUri)
                {
                    Stream stream = hwresp.GetResponseStream();
                    BasicFileSaver bis = new BasicFileSaver(UriToSubDirectory(m_u), UriToFilename(m_u), stream, m_u);
                    fSaved = await bis.SaveAsync(true, null, CancellationToken.None);
                }
            }
            catch
            {
            }
            return fSaved;
        }
Exemplo n.º 2
0
        public async Task<IRandomAccessStream> DownloadAsync(IProgress<int> progress, CancellationToken ct)
        {
            IRandomAccessStream iras = await DownloadAsyncFromFile(progress);
            if (iras != null)
            {
                return iras;
            }
            else
            {
                bool fSaved;

                try
                {
                    // Download from the inter-tubes
                    HttpWebRequest hwreq = WebRequest.Create(m_u) as HttpWebRequest;
                    HttpWebResponse hwresp = (await hwreq.GetResponseAsync()) as HttpWebResponse;

                    if (hwresp.ResponseUri.AbsoluteUri == m_u.AbsoluteUri)
                    {
                        Stream stream = hwresp.GetResponseStream();

                        // Save the file out locally - note this moves the stream forward to the end
                        BasicFileSaver bis = new BasicFileSaver(UriToSubDirectory(m_u), UriToFilename(m_u), stream, m_u);
                        Progress<ulong> newProgress = null;
                        if (progress != null)
                        {
                            newProgress = new Progress<ulong>((p) =>
                                                            {
                                                                var percent = (p * 100) / (ulong)hwresp.ContentLength;
                                                                progress.Report((int)percent);
                                                            });
                        }

                        fSaved = await bis.SaveAsync(false, newProgress, ct);
                        if (progress != null)
                        {
                            progress.Report(100);
                        }
                    }
                    else
                    {
                        fSaved = false;
                    }
                }
                catch
                {
                    fSaved = false;
                }

                // If we managed to save the file, then read off of the disk - from the place we just saved
                return fSaved ? await DownloadAsyncFromFile(progress) : null;
            }
        }