如何在webview xamarin.forms中打开远程路径pdf

时间:2016-08-22 11:32:56

标签: xamarin xamarin.ios xamarin.android xamarin.forms nuget-package

我在xamarin.forms工作。我得到了HTML内容作为josn Response。

<!-- THEME DEBUG --> <!-- CALL: theme('node') --> <!-- FILE NAME SUGGESTIONS: * node--253.tpl.php * node--article.tpl.php x node.tpl.php -->
<!-- BEGIN OUTPUT from 'sites/all/themes/maharastracmonew/templates/node.tpl.php' -->
<div id="node-253" class="node node-article clearfix" about="/maharastracmo/en/magazines" typeof="sioc:Item foaf:Document">
    <h2> <a href="/maharastracmo/en/magazines">Magazine Gallery</a> </h2>
    <span property="dc:title" content="Magazine Gallery" class="rdf-meta element-hidden"></span>
    <span property="sioc:num_replies" content="0" datatype="xsd:integer" class="rdf-meta element-hidden"></span>
    <div class="field field-name-body field-type-text-with-summary field-label-hidden">
        <div class="field-items">
            <div class="field-item even" property="content:encoded">
                <div class="innerContent">
                    <div class="pdfBlock">
                        <div class="pdfIconBox">
                            <a href="http://14.141.36.212/maharastracmo/sites/all/themes/maharastracmonew/pdf/MA-June15-binder-6.pdf" target="_blank">
                                <img alt="" src="http://14.141.36.212/maharastracmo/sites/all/themes/maharastracmonew/images/book-icon.png" />
                            </a>
                            <h5>Maharashtra Ahead</h5> <span class="bookDate">June 2015</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div> <!-- END OUTPUT from 'sites/all/themes/maharastracmonew/templates/node.tpl.php' -->

在此内容中有一个图像,当用户点击图像时,会在新浏览器中打开.pdf。

我创建html并在WebView中显示该html但在图像上单击pdf文件是不会打开的。 pdf文件来自远程设备。 (服务器)。

第二次尝试:

作为第二种选择,我采用了webview,只是将pdf远程路径作为源属性,但是空白页面是打开的。我该如何解决这个问题?

第三次尝试:

我只使用一个按钮并在按钮点击事件上pdf路径在另一个浏览器中打开。但是没有打开,而是直接下载pdf文件。

protected async void OnClicked(object sender, EventArgs e)
        {
            var uri = new Uri("http://14.141.36.212/maharastracmo/sites/all/themes/maharastracmonew/pdf/MA-June15-binder-6.pdf");
            Device.OpenUri(uri);
        }

1 个答案:

答案 0 :(得分:0)

需要使用Xamarin依赖服务。我就是这样做的。

首先定义一个界面:

namespace Mobile.DependencyService
{
    /// <summary>
    /// 
    /// </summary>
    public interface IDownload
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="bytes"></param>
        /// <param name="fullPathToSavedFile"></param>
        void Save(string name, byte[] bytes, out string fullPathToSavedFile);
    }
}

在点击事件中:var uri =您指向pdf的链接;

var uri = repository.GetResumeUri(model);

  if (Device.OS == TargetPlatform.Android)
  {
    using (var clientHandler = new System.Net.Http.HttpClientHandler())
    {
       using (var httpClient = new System.Net.Http.HttpClient(clientHandler))
       {
           httpClient.BaseAddress = uri;
           byte[] bytes = await httpClient.GetByteArrayAsync(uri);
           var service = Xamarin.Forms.DependencyService.Get<Mobile.DependencyService.IDownload>();

           string fullPathToSavedFile;
           service.Save(
                        String.Format("{0}.pdf", System.Guid.NewGuid().ToString("N")), //String.Format("{0} Resume.pdf", model.Type),
                         bytes,
                         out fullPathToSavedFile
                         );

           uri = new Uri(String.Format("file://{0}", fullPathToSavedFile));
        }
     }
  }
  Device.OpenUri(uri);

在iOS中:

[assembly: Xamarin.Forms.Dependency(typeof(Mobile.iOS.DependencyService.Download))]
namespace Mobile.iOS.DependencyService
{
    public class Download : IDownload
    {
      public void Save(string name, byte[] bytes, out string fullPathToSavedFile)
        {
            fullPathToSavedFile = String.Empty;

            try
            {
             fullPathToSavedFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), name);

                File.WriteAllBytes(fullPathToSavedFile, bytes);
            }
            catch (Exception ex)
            {
                var ex1 = ex;
            }
        }
    }
}

对于Android:

[assembly: Xamarin.Forms.Dependency(typeof(Mobile.Droid.DependencyService.Download))]
namespace Mobile.Droid.DependencyService
{
   public class Download : IDownload
    {
       public void Save(string name, byte[] bytes, out string fullPathToSavedFile)
        {
            fullPathToSavedFile = String.Empty;
            // https://developer.xamarin.com/api/type/System.Environment+SpecialFolder/
            // http://developer.android.com/guide/topics/data/data-storage.html

            try
            {

                //var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), name);
                using(var directory = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads))
                {
                    if (null != directory) 
                    {
                        var state = Android.OS.Environment.GetExternalStorageState(directory);

                        if (String.Compare(state, Android.OS.Environment.MediaMounted,true)==0)
                        {
                            fullPathToSavedFile = Path.Combine(directory.AbsolutePath, name);
                            File.WriteAllBytes(fullPathToSavedFile, bytes);

                            //File.WriteAllBytes(Path.Combine(directory.AbsolutePath, name), bytes);
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                var ex1 = ex;
            }
         }
       }
     }