Download and Save PDF for viewing

2019-05-10 08:43发布

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.

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);
}

2条回答
相关推荐>>
2楼-- · 2019-05-10 09:02

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

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);
}
查看更多
做个烂人
3楼-- · 2019-05-10 09:13

Here is the sample code to download file in PCL Xamarin from remote server. I have used PCLStorage library package which is available in Nuget. You just need download and install in your project.

 public  async void Downloadfile(string Url)
    {
        try
        {
            Uri url = new Uri(Url);
            var client = new HttpClient();


            IFolder rootfolder = FileSystem.Current.LocalStorage;
            IFolder appfolder = await rootfolder.CreateFolderAsync("Download", CreationCollisionOption.OpenIfExists);
            IFolder dbfolder = await appfolder.CreateFolderAsync("foldername", CreationCollisionOption.OpenIfExists);
            IFile file = await dbfolder.CreateFileAsync(strReport_name, CreationCollisionOption.ReplaceExisting);
            using (var fileHandler = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
            {
                var httpResponse = await client.GetAsync(url);
                byte[] dataBuffer = await httpResponse.Content.ReadAsByteArrayAsync();
                await fileHandler.WriteAsync(dataBuffer, 0, dataBuffer.Length);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
查看更多
登录 后发表回答