WP8.1在独立存储中下载文件

时间:2016-01-11 21:51:02

标签: c# windows-phone-8 download windows-phone-8.1

我正在尝试下载并在隔离存储中保存文件。 这是我尝试下载文件

Task.Run(async () => { await DownloadFileFromWeb(new Uri(@"http://main.get4mobile.net/ringtone/ringtone/ibMjbqEYMHUnso8MErZ_UQ/1452584693/fa1b23bb5e35c8aed96b1a5aba43df3d/stefano_gambarelli_feat_pochill-land_on_mars_v2.mp3"), "mymp3.mp3"); }).Wait();

        public static Task<Stream> DownloadFile(Uri url)
        {
            var tcs = new TaskCompletionSource<Stream>();
            var wc = new WebClient();
            wc.OpenReadCompleted += (s, e) =>
            {
                if (e.Error != null) tcs.TrySetException(e.Error);
                else if (e.Cancelled) tcs.TrySetCanceled();
                else tcs.TrySetResult(e.Result);
            };
            wc.OpenReadAsync(url);
            return tcs.Task;
        }

        public static async Task<Problem> DownloadFileFromWeb(Uri uriToDownload, string fileName)
        {
            using (Stream mystr = await DownloadFile(uriToDownload))
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream file = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, isf))
                {
                    using (var fs = new StreamWriter(file))
                    {
                        byte[] bytesInStream = new byte[mystr.Length];
                        mystr.Read(bytesInStream, 0, (int)bytesInStream.Length);
                        file.Write(bytesInStream, 0, bytesInStream.Length);
                        file.Flush();
                    }
                }
            }
            return Problem.Ok;
        }

显然我在这里做错了,因为文件永远不会在调用后应用程序永远堆栈。 但是我相信我离这儿不远。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

添加此methid并从那里调用它,它应该可以工作。

Downlaod_Click()
public static async void Downlaod_Click()
        {
            var cts = new CancellationTokenSource();
            Problem fileDownloaded = await MainHelper.DownloadFileFromWeb(new Uri(@"url", UriKind.Absolute), "myfile.mp3", cts.Token);
            switch (fileDownloaded)
            {
                case Problem.Ok:
                    MessageBox.Show("File downloaded");
                    break;
                case Problem.Cancelled:
                    MessageBox.Show("Download cancelled");
                    break;
                case Problem.Other:
                default:
                    MessageBox.Show("Other problem with download");
                    break;
            }
        }

答案 1 :(得分:-1)

IsolatedStorage不是available in windows 8.1。因此,您可以使用以下Windows 8.1应用程序代码,它可以正常工作:

 Task.Run(async () => { await DownloadFileFromWeb(new Uri(@"http://main.get4mobile.net/ringtone/ringtone/ibMjbqEYMHUnso8MErZ_UQ/1452584693/fa1b23bb5e35c8aed96b1a5aba43df3d/stefano_gambarelli_feat_pochill-land_on_mars_v2.mp3"), "mymp3.mp3"); }).Wait();

    public static async Task<Stream> DownloadFile(Uri url)
    {
        var tcs = new TaskCompletionSource<Stream>();

        HttpClient http = new System.Net.Http.HttpClient();
        HttpResponseMessage response = await http.GetAsync(url);

            MemoryStream stream = new MemoryStream();
            ulong length = 0;
            response.Content.TryComputeLength(out length);
            if (length > 0)
                await response.Content.WriteToStreamAsync(stream.AsOutputStream());

            stream.Position = 0;
            return stream;

    }

    public static async Task<string> DownloadFileFromWeb(Uri uriToDownload, string fileName)
    {
        using (Stream stream = await DownloadFile(uriToDownload))
        {
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
            var file = await local.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            stream.Position = 0;
            using (Stream fileStream = await file.OpenStreamForWriteAsync())
            {
                stream.CopyTo(fileStream);
            }
            return file.Path;
        }
    }