ManOfWar
ManOfWar

Reputation: 121

Xamarin Forms open PDF in browser

I want to get PDF file which is Base64 encoded and is presented as string. At first I save file locally on my mobile phone by this method and returns path to the file:

 byte[] byteBuffer = Encoding.UTF8.GetBytes(pdfString);

 public string SaveBinary(string filename, byte[] bytes)
        {
            string filepath = GetFilePath(filename);
            if (File.Exists((filepath)))
            {
                File.Delete(filepath);
            }
            File.WriteAllBytes(filepath, bytes);

            return filepath;
        }

string GetFilePath(string filename)
        {
            return Path.Combine(GetDocsPath(), filename);
        }

        string GetDocsPath()
        {
            return System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);

        }

Then I try to open PDF in browser but nothing happens.

  string filename = DependencyService.Get<IFileWorker>().SaveBinary($"name.pdf", byteBuffer);
                try
                {
                    Device.OpenUri(new Uri(filename));

Upvotes: 2

Views: 3903

Answers (1)

Related Questions