How to intercept and change image path when loading KML files (get around CORS)

489
2
Jump to solution
02-14-2023 11:07 AM
JasonK
by
New Contributor

I am able to load kml/kmz files using KmlLayer of the v4.25 javascript SDK.  The issue is some images within the KML cause CORS errors.  I would like to programmatically update the image URL to a locally hosted copy of the image during kml layer load.  I just can't determine where/how to find the images within the layer.

Code to load the layer:

      let layer_KML_test: KmlLayer = new KmlLayer({
        url: {public url to kml},
        title: layerFile.name,
      });
 
Error:

[esri.views.2d.engine.webgl.mesh.templates.WGLTemplateStore] s {name: 'mapview-invalid-resource', details: undefined, message: 'Could not fetch requested resource at https:…ages/modis_mod14/modis_prev_6_days_fire.png.'}

I want to change the path from: 

https://fsapps.nwcg.gov/afm/data/kml/images/modis_mod14/modis_prev_6_days_fire.png

To:

https://{myappdomain.com}/images/modis_prev_6_days_fire.png

 

Tags (4)
0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

It seems to me you could use esriConfig.request.interceptors to do this.  If so, it would look something like:

require(["esri/config", function(esriConfig) {
	esriConfig.request.interceptors.push({
		urls: "https://fsapps.nwcg.gov/afm/data/kml/images/",
		before: function(params) {
			params.url = "https://myServer/images" + params.url.substr(params.url.lastIndexOf("/"));

			return null;
		}
	});
});

View solution in original post

0 Kudos
2 Replies
JoelBennett
MVP Regular Contributor

It seems to me you could use esriConfig.request.interceptors to do this.  If so, it would look something like:

require(["esri/config", function(esriConfig) {
	esriConfig.request.interceptors.push({
		urls: "https://fsapps.nwcg.gov/afm/data/kml/images/",
		before: function(params) {
			params.url = "https://myServer/images" + params.url.substr(params.url.lastIndexOf("/"));

			return null;
		}
	});
});
0 Kudos
JasonK
by
New Contributor

I think this is what I'm looking for.  Thanks for the quick reply.

0 Kudos