1

I need to download files like ".Doc , .pdf , .xls , .Jpeg, .PNG etc" from the server and store into phone memory not to Isolated. I have search a lot but can not get any things for .doc , .pdf. I got a link Downloading and saving a file Async in Windows Phone 8 but can not work. So if any one can do this please let me know. Thanks in advance.

Community
  • 1
  • 1
Maulik Shah
  • 673
  • 7
  • 19

2 Answers2

2

I have done with FileSavePicker , here is code

    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);
    }

For More information Please go with this url How to continue your Windows Phone app after calling a file picker

Maulik Shah
  • 673
  • 7
  • 19
  • Can you help me with WP 8.1 non silverlight ? – Macaret Feb 10 '15 at 19:17
  • Please See this code https://code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba for file picker in Universal app which works for both Windows 8 store app and Windows Phone 8.1 Store app(non silver Light) – Maulik Shah Feb 11 '15 at 03:42
0

I don't think you'll be able to directly download and store the files into the memory card as they have restricted access for security purposes. I guess Isolated Storage could be the option.

Reference

Kulasangar
  • 9,046
  • 5
  • 51
  • 82