locked
Load Local HTML file in Web View control from the temp folder location.

    Question

  • Hi 

    I want to load the HTML files in Webview control. I have the files in Temp Folder or Local folder. But when i try to open this HTML file using Web View control it does not load the file. But i was able to open the same file in the browser without any issue. 

       var uri = new Uri(decryptedFile.Path, UriKind.RelativeOrAbsolute);
                    WebControl.Navigate(uri);

    Thanks in Advance.

    Thanks,

    Kamal 


    -Kamal. If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, April 08, 2014 9:54 PM

Answers

  • Hi KamalWP7,

    While navigating to a local page, this method NavigateToLocalStreamUri method is suggested.

    public sealed partial class TestPage : Page
    {
        // ... other code ...
    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // The 'Host' part of the URI for the ms-local-stream protocol needs to be a combination of the package name
            // and an application-defined key, which identifies the specific resolver, in this case 'MyTag'.
                
            Uri url = webView4.BuildLocalStreamUri("MyTag","/Minesweeper/default.html");
            StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
    
            // Pass the resolver object to the navigate call.
            webView4.NavigateToLocalStreamUri(url, myResolver);
        }
    }
    
    public sealed class StreamUriWinRTResolver : IUriToStreamResolver
    {
        public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
        {
            if (uri == null)
            {
                throw new Exception();
            }
            string path = uri.AbsolutePath;
    
            // Because of the signature of the this method, it can't use await, so we 
            // call into a seperate helper method that can use the C# await pattern.
            return GetContent(path).AsAsyncOperation();
        }
    
        private async Task<IInputStream> GetContent(string path)
        {
            // We use a package folder as the source, but the same principle should apply
            // when supplying content from other locations
            try
            {
                Uri localUri= new Uri("ms-appx:///html" + path);
                StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
                IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
                return stream;
            }
            catch (Exception) { throw new Exception("Invalid path"); }
        }
    }
    
    

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, April 09, 2014 1:21 AM
    Moderator