【问题标题】:Download files from web and save in Phone : Windows Phone 8.1 (SilverLight) Development从网络下载文件并保存在手机中:Windows Phone 8.1 (SilverLight) 开发
【发布时间】:2015-02-07 11:46:11
【问题描述】:

我需要从服务器下载 ".Doc , .pdf , .xls , .Jpeg, .PNG etc" 等文件并存储到手机内存中而不是隔离。我进行了很多搜索,但找不到 .doc 、 .pdf 的任何内容。我有一个链接Downloading and saving a file Async in Windows Phone 8 但不能工作。因此,如果有人可以做到这一点,请告诉我。 提前致谢。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    我认为您不能直接下载文件并将其存储到存储卡中,因为出于安全目的,它们的访问权限受到限制。我猜Isolated Storage 可能是一个选项。

    Reference

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 1970-01-01
      • 2015-06-25
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      相关资源
      最近更新 更多