从Web下载文件并保存在手机中:Windows Phone 8.1(SilverLight)开发

时间:2015-02-07 11:46:11

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

我需要从服务器下载" .Doc,.pdf,.xls,.Jpeg,.PNG等" 等文件,并将其存储到手机内存中,而不是隔离。我搜索了很多,但无法获得.doc,.pdf的任何内容。我收到了一个链接Downloading and saving a file Async in Windows Phone 8 但无法奏效。所以如果有人可以这样做,请告诉我。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

我已经完成了FileSavePicker,这里是代码

    public void DownloadFiles(Uri url)
    {

        var wc = new WebClient();
        wc.OpenReadCompleted +=async (s, e) =>
        {
            Stream st = e.Result;
            buf = ReadFully(st);
            FileSavePicker savePicker = new FileSavePicker();

            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

            // Dropdown of file types the user can save the file as
            savePicker.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" });

            // Default file name if the user does not type one in or select a file to replace
            savePicker.SuggestedFileName = "New Document";

            savePicker.PickSaveFileAndContinue();


            StorageFile SF = await KnownFolders.PicturesLibrary.CreateFileAsync
                      ("Guide.pdf", CreationCollisionOption.ReplaceExisting);
            var fs = await SF.OpenAsync(FileAccessMode.ReadWrite);
            StorageStreamTransaction transaction = await SF.OpenTransactedWriteAsync();
            DataWriter dataWriter = new DataWriter(transaction.Stream);
            dataWriter.WriteBytes(buf);
            transaction.Stream.Size = await dataWriter.StoreAsync(); // reset stream size to override the file
            await transaction.CommitAsync();
        };
        wc.OpenReadAsync(url);

    }

  public static byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }
   private async void ContinueFileOpenPicker(FileSavePickerContinuationEventArgs args)
    {
        StorageFile file = args.File;
        if (file != null)
        {
            // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
            CachedFileManager.DeferUpdates(file);
            // write to file
            await FileIO.WriteBytesAsync(file, buf);
            // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
            if (status == FileUpdateStatus.Complete)
            {
                Debug.WriteLine("File " + file.Name + " was saved.");
            }
            else
            {
                Debug.WriteLine("File " + file.Name + " couldn't be saved.");
            }
        }
        else
        {
            Debug.WriteLine("Operation cancelled.");
        }
        await Windows.System.Launcher.LaunchFileAsync(file);
    }

更多信息请使用此网址How to continue your Windows Phone app after calling a file picker

答案 1 :(得分:0)

我认为您无法直接将文件下载并存储到存储卡中,因为出于安全考虑,它们会限制访问。我猜Isolated Storage可能是选项。

Reference