Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Passing Images to js

Post Reply
Lion85
Posts: 15
Joined: Tue Dec 06, 2022 9:19 am

Passing Images to js

Post 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.
User avatar
salvadordf
Posts: 4079
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Passing Images to js

Post 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;
Lion85
Posts: 15
Joined: Tue Dec 06, 2022 9:19 am

Re: Passing Images to js

Post 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.
User avatar
salvadordf
Posts: 4079
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Passing Images to js

Post 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.
Lion85
Posts: 15
Joined: Tue Dec 06, 2022 9:19 am

Re: Passing Images to js

Post 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.
Post Reply