I want to load local html file which in the local folder to the webview, but WebView doesn't support 'ms-aspx:///' protocal, I found a solution to read the html file to stream, and then convert it to string, using NavigateToString method to load the html, it works well. But, If there's an image in the html file, the image couldn't display, anyone can help?
Asked
Active
Viewed 1,465 times
2 Answers
5
I have solved.
Solution:
Convert the image file to base64 string
StorageFolder appFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await appFolder.GetFileAsync("SplashScreen.png");
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
var reader = new DataReader(stream.GetInputStreamAt(0));
var bytes = new byte[stream.Size];
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(bytes);
base64 = Convert.ToBase64String(bytes);
}
Use StringBuilder to create the html string
sb.Append("<html><head><title>Image test</title></head><body><p>This is a test app!</p><img src=\"data:image/png;base64,");
sb.Append(base64);
sb.Append("\" /></body></html>");
TestWebView.NavigateToString(sb.ToString());

Allen4Tech
- 2,094
- 3
- 26
- 66
-
Weeks of research in trying to display images in a local html file within the WinRT environment and you absolutely killed it with this answer! FYI my solution is not using a web viewer but creating the file dynamically then launching it. You can see my solution [here](http://stackoverflow.com/questions/24197460/how-to-create-report-for-windows-store-app/27387332#27387332) – Anthony Griggs Mar 14 '15 at 23:28
0
Try using the ms-appx-web:// scheme instead of ms-aspx:// to load html from a WebView. If that doesn't work, you may need to use the ms-appdata:// scheme to access the image if it's in your application data folder.
Some further resources that might help:
How to load a local HTML-File into Webview
URI schemes
How to reference content

Jennifer Marsman - MSFT
- 5,167
- 1
- 25
- 24
-
I'ts a local html file, so I can't use ms-appx-web, webview doesn't support other schemas. I convert the content of the html to string and then use NavigateToString method to load the html, the content works well except image. – Allen4Tech Jan 21 '13 at 13:24