0

我正在使用xamarin.forms。我将HTML内容作为josn Response。如何在webview中打开远程路径pdf xamarin.forms

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

在iOS 9.0或更高版本ATS限制落实到位,以阻止在默认情况下非HTTPS引用。在输出窗口中查找网络错误。您可以查看[这里](https://developer.xamarin.com/guides/ios/platform_features/introduction_to_ios9/ats/#Opting-Out_of_ATS),了解如何从这些限制中排除特定的网址。在Android上,WebViews默认无法打开PDF。您需要先下载它们才能显示它们。 – hvaughan3

+0

在哪个平台上遇到此问题?在Android上? Android的'WebView'不支持开箱即用。你需要'loadUrl',你只需要一个依赖服务就可以得到。 – testing

回答

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

对于安卓

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

我不想保存文件。我只想打开文件。 –

+0

如果我没有错,数据会被缓存,并在之后被删除。 – nishantvodoo

相关问题