locked
Local folder - WebView and IE security zone issues

    Question

  • Hi,

    I'm dealing with problem, that totally stopped my development.

    I would like to show html page in WebView control. I download data from server, save them to LocalFolder and use this piece of simple code 

    string url = "ms-appdata:///local/FolderWithPageData/index.html";   

    WebView.Navigate(new Uri(url));

    Index.html uses files that are downloaded in separated folders (css, js, pictures, fonts etc.). WebView can't load them and shows only plain text of index.html body. 

    I tried to explore what is happening and I found one solution. To turn off Security level (or disable protected mode) in IE. After this change in IE setting everything start to work, but I would like to have Security level in IE allowed. Another point is, that my app should regardless all settings in IE.

    I read one solution to save files in installation folder of my Windows store app, but I don't like this solution so much.

    Please, can you help me?

    Thanks.


    Friday, June 20, 2014 2:34 PM

Answers

  • Hi Jozef368,

    Try with this API: NavigateToLocalStreamUri method.

    Use this method to load local content that the NavigateToString method won't handle. NavigateToString provides an easy way to navigate to static HTML content, including content with references to resources such as CSS, scripts, images, and fonts. However, NavigateToString does not provide a way to generate these resources programmatically.

    some code from MSDN:

    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.

    Monday, June 23, 2014 8:11 AM
    Moderator