【问题标题】:How to open remote path pdf in webview xamarin.forms如何在 webview xamarin.forms 中打开远程路径 pdf
【发布时间】:2016-08-22 11:32:56
【问题描述】:

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

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

【问题讨论】:

  • 在 iOS 9.0+ 上,ATS 限制默认设置为阻止非 HTTPS 引用。在输出窗口中查找网络错误。您可以查看 here 以了解如何从这些限制中排除特定 URL。在 Android 上,默认情况下 WebView 无法打开 PDF。您需要先下载它们才能显示它们。
  • 您在哪个平台上遇到了这个问题?在安卓上? Android 的WebView 不支持开箱即用。你需要loadUrl,你只能通过依赖服务或其他东西获得。

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


【解决方案1】:

需要使用 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;
            }
        }
    }
}

对于安卓:

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

【讨论】:

  • 我不想保存文件。我只想打开文件。
  • 如果我没记错的话,数据被缓存,之后被删除。
猜你喜欢
  • 2016-05-30
  • 2013-05-09
  • 2015-07-30
  • 2021-12-28
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 2021-03-01
  • 1970-01-01
相关资源
最近更新 更多