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

相關問題