0

私はxamarin.formsで働いています。 josnレスポンスとしてHTMLコンテンツを取得しました。webview xamarin.formsでリモートパスpdfを開く方法

<!-- 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' --> 

はこのコンテンツでは、1枚の画像と画像.PDF上のユーザーのクリックは新しいブラウザで開いているときにそこにあります。

私はhtmlを作成し、そのHTMLをWebViewに表示しますが、画像をクリックするとpdfファイルは開かれません。 pdfファイルはリモートデバイスからのものです。 (サーバ)。

2回目の試行:私はWebViewのを取られ、ソースプロパティが、空白のページが開いていると、単純にPDFファイルのリモートパスを入れている第二の選択肢として

。どうすればこの問題を解決できますか?

3回目:

私は単純に一つのボタンを使用し、ボタンをクリックしてイベント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

は、デフォルトでは非HTTPSの参照をブロックするような場所に置かれています。出力ウィンドウでネットワークエラーを調べます。これらの制限から特定のURLを除外する方法については、[こちら](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 Dependency Serviceを使用する必要があります。ここで私がそれをした方法があります。あなたのクリックイベント内部

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に。 iOS版で

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

:Android用

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

:iOSの9.0以降ATSの制限に

[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

関連する問題