0

I have been using embedio and I would like to post pdf and display it in my Webview in my xamarin application. The pdf is as embedded resource in my application. It all seems to be ok, but the stream position is 0, however the status is 200 ok. and then At readTimeout I see

System.InvalidOperationException: Timeouts are not supported on this stream. at System.IO.Stream.get_ReadTimeout () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/shared…

My initialization of embedio

 public App()
                    {
                        
                        Task.Factory.StartNew(async () =>
                        {
                            using (var server = new WebServer(HttpListenerMode.EmbedIO, "http://*:8089"))
                            {
                                Assembly assembly = typeof(App).Assembly;
                                server.WithLocalSessionManager();
                                server.WithWebApi("/api", m => m.WithController(() => new PdfSourceController()));
                                server.WithEmbeddedResources("/", assembly, "TestApp.html");
                                await server.RunAsync();
                            }
                        });
             MainPage = new NavigationPage(new MainPage());
            }
        
        My controller
        
        
         public class PdfSourceController : WebApiController
            {
                public PdfSourceController() : base()
                {
                   
                }
              [Route(HttpVerbs.Post, "/pdf")]
            public async Task UploadFile()
            {
    
                var parser = new MultipartFormDataContent();
    
                var fileContent = new StreamContent(new EmbeddedResourceDataStream("Assets/TestDoc.pdf").GetDataStream());
                fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {Name = "result", FileName = $"\"{"test"}\""};
                fileContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
    
                parser.Add(fileContent);
            }
    
     private async Task TestPost()
            {
                try
                {
                    var handler = new HttpClientHandler
                    {
                        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate ,
                    };
                    using (var client = new HttpClient(handler))
                    {
                        client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("*"));
                   
                       
                        using (var response = await client.PostAsync($"{DefaultUrl}api/pdf", new MultipartFormDataContent() ).ConfigureAwait(false)) // calling controller?
                        {
                            response.Content.Headers.ContentType = new MediaTypeHeaderValue("api/pdf"); // stating what media type? 
                            var responseString = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); 
                            response.Content = new StreamContent(responseString);
                            Pdf = ImageSource.FromStream(() => responseString);
    
                        }
                    }
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
  • you want your embedded web server to return a PDF and display it in an embedded webview? That seems unnecessarily complex, your webview can just display a local PDF without needing a server. However, the first thing I'd look into is `EmbeddedResourceDataStream("Assets/TestDoc.pdf")` - an Android Asset is not an Embedded Resource, so I suspect this line will fail – Jason Jun 25 '21 at 21:13
  • I have this assets in shared, is not android assets its just a folder in my shared project – KuramaUzumaki Jun 28 '21 at 06:14

0 Answers0