简体   繁体   中英

Download files from web and save in Phone : Windows Phone 8.1 (SilverLight) Development

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM