0

저는 xamarin.forms에서 일하고 있습니다. josn 응답으로 HTML 콘텐츠가 있습니다.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 파일은 원격 장치에서 온 것입니다. (섬기는 사람).

두 번째 시도 : 소스 속성하지만 빈 페이지로 내가 웹보기를 촬영 한 두 번째 옵션 간단하게 넣어 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

는 기본적으로 비 HTTPS 참조를 차단하는 장소에 배치된다. 출력 창에서 네트워크 오류를 확인하십시오. 이러한 제한에서 특정 URL을 제외하는 방법은 [여기] (https://developer.xamarin.com/guides/ios/platform_features/introduction_to_ios9/ats/#Opting-Out_of_ATS)를 참조하십시오. Android에서는 WebView가 기본적으로 PDF를 열 수 없습니다. 먼저 다운로드해야 표시 할 수 있습니다. – hvaughan3

+0

어떤 플랫폼에서이 문제가 발생합니까? 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로; 아이폰 OS에서

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

: 안드로이드에 대한

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

: 아이폰 OS 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