display PDF in webview not working Xamarin forms

MehdiMllMehdiMll Member ✭✭
edited June 25 in Xamarin.Forms

i am trying to display a hosted IMAGE from a secure web api it working

    <WebView
                    HorizontalOptions="FillAndExpand"
                    Source="*://192.168.137.1:3581/Uploads/04-02_1.jpg"
                    VerticalOptions="FillAndExpand" />

but trying to display a hosted PDF from a secure web api it not working

<WebView
                HorizontalOptions="FillAndExpand"
                Source="*://192.168.137.1:3581/Uploads/3.pdf"
                VerticalOptions="FillAndExpand" />

*= http
Tagged:

Best Answer

  • MehdiMllMehdiMll ✭✭
    edited July 3 Accepted Answer

    i use method from APi for get file

           [HttpGet, AllowAnonymous, Route("api/File/{id}")]
            public async Task<HttpResponseMessage> GetFile(long id)
            {
                var file = await context.File.FindAsync(id);
                var data = file?.SystemFileName;
                System.IO.MemoryStream memoryStream = new MemoryStream();
                if (data != null && data.Length > 10)
                {
                    var localPhotoData = File.ReadAllBytes(data);
                    memoryStream = new System.IO.MemoryStream(localPhotoData);
                }
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StreamContent(memoryStream)
                };
                var contentType = MimeMapping.GetMimeMapping(file.OriginalFileName);
                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
                return response;
            }
    

    and Call It in Mobile projrct

    var Id = FileId;
    Device.OpenUri(new Uri(Constants.BaseApiAddress + "api/File/" + Id));
    i dont use
    await Xamarin.Essentials.Browser.OpenAsync(UrlFile);

    because it working just with Pdf
    https://github.com/xamarin/Essentials/issues/601

Answers

Sign In or Register to comment.