Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load local HTML into WebView

Can I load a local HTML file (with images and ...) into a WebView?
Just setting the Source parameter does not do the trick.

like image 680
MBZ Avatar asked Nov 05 '12 04:11

MBZ


People also ask

How do I open HTML file in WebView in flutter?

Next we will create the UI by adding the webview. Below is the full source code. Here we are loading the webview with empty Url at first and when the webview returns with the 'onWebViewCreated' callback, we will initialize the WebViewController which is then used to load the Local HTML file.

Where should I put HTML file in Android Studio?

Step 1: To add a local HTML file into your Android project there must be an asset folder in it. To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.


2 Answers

You can load it from a file as long as the file is part of the app package, e.g.:

WebView2.Source = new Uri("ms-appx-web:///assets/text.html");

From WebView.Navigate

WebView can load content from the application’s package using ms-appx-web://, from the network using http/https, or from a string using NavigateToString. It cannot load content from the application’s data storage. To access the intranet, the corresponding capability must be turned on in the application manifest.

For a 'random' file, I suppose you could prompt user via file picker to select the file then read it into a string and use NavigateToString, but the user experience there may be a bit odd depending on what you're trying to accomplish.

like image 65
Jim O'Neil Avatar answered Oct 24 '22 04:10

Jim O'Neil


I was working at this problem for a long time and I found a way to do that: At first you should save it in InstalledLocation folder. If you haven't option to create a new .html file you can just use file.CopyAsync(htmlFolder, fname + ".html"); Look into my example:

StorageFolder htmlFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFolderAsync(@"HtmlFiles", CreationCollisionOption.GenerateUniqueName);
IStorageFile file = await htmlFolder .CreateFileAsync(fname + ".html", CreationCollisionOption.GenerateUniqueName);

and than you can easily open your .html file:

var fop = new FileOpenPicker();
fop.FileTypeFilter.Add(".html");
var file = await fop.PickSingleFileAsync();
if (file != null)
{
   string myPath = file.Path.Substring(file.Path.IndexOf("HtmlFiles"));
   myWebview.Navigate(new Uri("ms-appx-web:///" + myPath));
}

Remember just only from InstalledLocation you can open it with ms-appx-web:///

like image 20
POF_Andrew_POF Avatar answered Oct 24 '22 04:10

POF_Andrew_POF