Erick
Erick

Reputation: 1247

winrt: load local html and its resources in webview

I'm currently developing a WinRT app where I load local HTML files from Windows.Storage.ApplicationData.Current.LocalFolder. I like to know what would be the best way to load assets like Images, Audio, Video, CSS, JavaScripts, etc, that are embedded in the HTML file.

Currently, I'm writing these files in the Windows.ApplicationModel.Package.Current.InstalledLocation but this wont be accessible anymore once the application had been deployed from the store or by installing the AppPackage through Windows PowerShell, unless you change the ownership of the "Program Files/WindowsApps" folder to the current user or to "Everyone".

Thanks in advance.

Upvotes: 3

Views: 3438

Answers (1)

Damir Arh
Damir Arh

Reputation: 17855

I'm not sure what exactly you're asking here. The only files that should be Windows.ApplicationModel.Package.Current.InstalledLocation are the ones that are installed with your AppPackage, i.e. are a part of your app distribution. Your app shouldn't (and is even not allowed) to store any of its files there.

Any files that are generated at runtime should be stored in one of Windows.Storage.ApplicationData.Current.LocalFolder, Windows.Storage.ApplicationData.Current.RemoteFolder and Windows.Storage.ApplicationData.Current.TempFolder, depending on how you want them to be preserved. The only exception to this rule is the case when you want the user to access these files even from outside your app - then you should put them in a library (and require access to it) or ask the user for a location using a file picker.

EDIT:

Loading local HTML resources is very much limited unless they are distributed along with the application, i.e. they are stored in InstalledLocation and accessed via ms-appx-web protocol. As documented, WebView control doesn't support ms-appdata protocol which is used to retrieve data from LocalStorage, RemoteStorage or TempFolder:

Furthermore, WebView does not support the ms-appdata: scheme, although it does support the ms-appx-web: scheme. This enables you to load content files in your project.

This more or less takes away all your options in C# projects. You could use an alternative HTML viewer control, but I know of only one and it isn't even finished yet.

In JavaScript the situation is only slightly better. ms-appdata protocol is supported only for data files:

Sometimes it is useful to refer to resources you have downloaded from the Internet to your app’s local ApplicationData storage (via Windows Runtime APIs). To refer to such content, you must use the scheme "ms-appdata:", with the path to the file within your ApplicationData local storage. Note that, for security reasons, you cannot navigate to HTML you have downloaded to this location and you cannot run any executable or potentially executable code, such as script or CSS. It is intended for media such as images or videos and the like.

This means you can do:

<img src="ms-appdata:///local/img/coal.jpg" />

But not:

<iframe src="ms-appdata:///local/index.html" />

ms-appdata protocal syntax is well documented in case you will still find use for it.

Upvotes: 9

Related Questions