繁体   English   中英

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

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

我需要从服务器下载“ .Doc,.pdf,.xls,.jpeg,.PNG等”文件 ,并将其存储到手机内存中而不要隔离。 我已经搜索了很多,但无法获得.doc,.pdf的任何内容。 我有一个链接该链接在Windows Phone 8中下载并保存文件异步,但无法正常工作。 因此,如果有人可以做到这一点,请告诉我。 提前致谢。

我已经完成了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);
    }

有关更多信息,请使用此URL。 如何在调用文件选择器后继续Windows Phone应用

我认为您不能直接下载文件并将其存储到存储卡中,因为出于安全目的它们具有受限访问权限。 我想可以选择“ Isolated Storage ”。

参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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