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

C# 从web下载文件并保存在手机中:Windows Phone 8.1(SilverLight)开发,c#,windows-phone-8,windows-phone-8.1,download,C#,Windows Phone 8,Windows Phone 8.1,Download,我需要从服务器下载文件,如“”.Doc、.pdf、.xls、.Jpeg、.PNG等”,并将其存储到手机内存中。我已经搜索了很多,但是找不到.doc、.pdf的任何东西。我有一个链接 但不能工作。因此,如果有人能做到这一点,请让我知道。 提前感谢。我认为您无法直接下载文件并将其存储到存储卡中,因为出于安全目的,这些文件的访问受到限制。我想隔离存储可能是一种选择 我已经完成了FileSavePicker,下面是代码 public void DownloadFiles(Uri url)

我需要从服务器下载文件,如“”.Doc、.pdf、.xls、.Jpeg、.PNG等”,并将其存储到手机内存中。我已经搜索了很多,但是找不到.doc、.pdf的任何东西。我有一个链接 但不能工作。因此,如果有人能做到这一点,请让我知道。
提前感谢。

我认为您无法直接下载文件并将其存储到存储卡中,因为出于安全目的,这些文件的访问受到限制。我想
隔离存储
可能是一种选择


我已经完成了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);
    }
公共无效下载文件(Uri url)
{
var wc=新的WebClient();
wc.OpenReadCompleted+=异步(s,e)=>
{
流st=e.结果;
buf=准备就绪(st);
FileSavePicker savePicker=新FileSavePicker();
savePicker.SuggestedStartLocation=PickerLocationId.DocumentsLibrary;
//用户可以将文件保存为的文件类型下拉列表
savePicker.FileTypeChoices.Add(“PDF”,newlist(){.PDF});
//如果用户未键入文件名或未选择要替换的文件,则为默认文件名
savePicker.SuggestedFileName=“新建文档”;
savePicker.PickSaveFileAndContinue();
StorageFile SF=等待KnownFolders.PicturesLibrary.CreateFileAsync
(“Guide.pdf”,CreationCollisionOption.ReplaceExisting);
var fs=await SF.OpenAsync(FileAccessMode.ReadWrite);
StorageStreamTransaction=wait SF.openTransacticedWriteAsync();
DataWriter DataWriter=新的DataWriter(transaction.Stream);
dataWriter.WriteBytes(buf);
transaction.Stream.Size=wait dataWriter.StoreAsync();//重置流大小以覆盖文件
wait transaction.CommitAsync();
};
OpenReadAsync(url);
}
公共静态字节[]已就绪(流输入)
{
字节[]缓冲区=新字节[16*1024];
使用(MemoryStream ms=new MemoryStream())
{
int-read;
而((read=input.read(buffer,0,buffer.Length))>0)
{
ms.Write(缓冲区,0,读取);
}
返回ToArray女士();
}
}
专用异步void ContinueFileOpenPicker(FileSavePickerContinuationEventArgs args args)
{
StorageFile=args.file;
如果(文件!=null)
{
//在完成更改并调用CompleteUpdatesAsync之前,禁止更新文件的远程版本。
CachedFileManager.DeferUpdates(文件);
//写入文件
wait FileIO.WriteBytesAsync(文件,buf);
//让Windows知道我们已完成更改文件,以便其他应用程序可以更新文件的远程版本。
//完成更新可能需要Windows请求用户输入。
FileUpdateStatus status=await CachedFileManager.CompleteUpdatesAsync(文件);
if(status==FileUpdateStatus.Complete)
{
Debug.WriteLine(“文件”+File.Name+“已保存”);
}
其他的
{
Debug.WriteLine(“文件”+文件名+“无法保存”);
}
}
其他的
{
Debug.WriteLine(“操作已取消”);
}
等待Windows.System.Launcher.LaunchFileAsync(文件);
}

有关更多信息,请使用此url

您能帮助我使用WP 8.1非silverlight吗?请参阅通用应用程序中的文件选取器代码,该程序适用于Windows 8应用商店应用程序和Windows Phone 8.1应用商店应用程序(非silverlight)