下载并保存 PDF 以供查看 [英] Download and Save PDF for viewing

查看:26
本文介绍了下载并保存 PDF 以供查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的应用程序下载 PDF 文档并将其显示在 IBooks 中,或者至少在完成下载后使其可供阅读.

Im trying to download a PDF document from my app and display it in IBooks or at least make it available to read some how when its completed downloading.

我遵循了 Xamarin 的下载示例,它允许我下载 PDF 并将其保存在本地.它也被保存在错误的编码中.

I followed the download example from Xamarin which allows me download the PDF and save it locally. Its being save in the wrong encoding also.

这是我迄今为止尝试过的.

This is what I've tried so far.

private void PdfClickHandler()
{
    var webClient = new WebClient();

    webClient.DownloadStringCompleted += (s, e) => {
        var text = e.Result; // get the downloaded text
        string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string localFilename = $"{_blueways}.pdf";
        // writes to local storage
        File.WriteAllText(Path.Combine(documentsPath, localFilename), text); 

        InvokeOnMainThread(() => {
            new UIAlertView("Done", "File downloaded and saved", null, "OK", null).Show();
        });
    };

    var url = new Uri(_blueway.PDF);
    webClient.Encoding = Encoding.UTF8;
    webClient.DownloadStringAsync(url);
}

推荐答案

不要将 DownloadStringAsync 用于二进制"数据,使用 DownloadDataAsync:

Do not use DownloadStringAsync for "binary" data, use DownloadDataAsync:

从指定为异步操作的 URI 中以字节数组的形式下载资源.

Downloads the resource as a Byte array from the URI specified as an asynchronous operation.

private void PdfClickHandler ()
{
    var webClient = new WebClient ();

    webClient.DownloadDataCompleted += (s, e) => {
        var data = e.Result;
        string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string localFilename = $"{_blueways}.pdf";
        File.WriteAllBytes (Path.Combine (documentsPath, localFilename), data);
        InvokeOnMainThread (() => {
            new UIAlertView ("Done", "File downloaded and saved", null, "OK", null).Show ();
        });
    };
    var url = new Uri ("_blueway.PDF");
    webClient.DownloadDataAsync (url);
}

这篇关于下载并保存 PDF 以供查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆