Page 1 of 1

Passing Images to js

Posted: Wed Mar 08, 2023 8:19 am
by Lion85
Hello, everyone,
In my programme I pass an image to overlay on my webview page.
When I retrieve the file from the program in c++ and pass it to js, the browser returns this error:

Code: Select all

Not allowed to load local resource:
Do you have an idea how I can solve this?
I've tried

Code: Select all

AdditionalBrowserArguments = "--allow-insecure-localhost";
but nothing, I can't solve it.

Re: Passing Images to js

Posted: Wed Mar 08, 2023 3:11 pm
by salvadordf
Hi,

Please, check the Google results when you search "Javascript Not allowed to load local resource" because there are several fixes and workarounds.

Try adding this code line before the GlobalWebView2Loader.StartWebView2 call :

Code: Select all

GlobalWebView2Loader.AllowFileAccessFromFiles := True;

Re: Passing Images to js

Posted: Thu Mar 09, 2023 1:47 pm
by Lion85
salvadordf wrote: Wed Mar 08, 2023 3:11 pm GlobalWebView2Loader.AllowFileAccessFromFiles := True;
I cannot find a good solution on the Internet.
If I add your code, the programme goes into error:

Code: Select all

WebView2: Initialization failed due to incompatible environment configurations. Please check if there is already a WebView2 running with the same user data folder but different environment parameters.

Re: Passing Images to js

Posted: Sat Mar 11, 2023 10:31 am
by salvadordf
I tried the AllowFileAccessFromFiles property with one of the demos and it doesn't show that error.
If you run several applications with WebView2 browsers and all of them share the same user data directory then check that all them use the same property value.

In any case, it would be helpful if you could provide a minimal code example that I can build to reproduce this issue in my computer.

Re: Passing Images to js

Posted: Tue Mar 14, 2023 9:12 am
by Lion85
Hi friend, thanks for the availability.
I solved in another way.
I used the SetVirtualHostNameToFolderMapping function and so far it is working great.

I briefly explain what I did, maybe someone might need.

1. I create a temporary folder and put the images I need in it.
2. I create the "Virtual Server"
I set the variables

Code: Select all

hostname = "example.host"
folderPath = "tempPath"
accessKing = Uwvtypelibrary::COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_ALLOW || COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY || COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS 
browser->SetVirtualHostNameToFolderMapping(hostName,folderPath,accessKind);
4. in my js I load the image with the following path: example.host/img.png

PS. the setVirtualHost function must be created before creating the page.
When I close the window of interest I destroy the "Virtual Host " with

Code: Select all

browser->ClearVirtualHostNameToFolderMapping(hostname);

//hope I have explained well what I have done, so that I can help other users.