How to deal with KML files

In my non-Kirby website, if a reader clicks on the link generated by the following code


<a href=" /climate/the-route/triverroute.kml">Right click on this link to save a Google Earth kml file of the route.</a>

results in Google Earth opening the file if the site visitor has it installed, or offers to download the file if they do not have Google Earth installed. And if they right click, they also have the opportunity to download the file.

But in Kirby if they simply left-click on the link it throws an error message: " Not Found The requested URL was not found on this server." This is hardly an ideal outcome.

And I cannot use the panel to load the kml file because “the media type “text/html” is not allowed uploading a kml file” I have to manually copy the file into the correct page folder.

Is there anyway around these two issues? I have dozens of these files I wish to link to.

Andrew Prior

I dont think KML is in the list of built in mime types. However, you can register a new file type in the config like this:

The reason the links dont work is because you need to use a file object. Assuming you have stored the KML file in a files field called “googleearth”, you can get its url like this:

<?php if($kml = $page->googleearth()->toFile()): ?>
  <a href="<?= $kml->url() ?>">Right click on this link to save a Google Earth kml file of the route.</a>
<?php endif ?>

I don’t think it is necessary to create a new file type. If you assign a file blueprint to your files section or field, with

accept:
    extension: kml

you can upload .kml files.

about the first issue, does it work if you remove the extra space in front of the path?

<a href=" /climate/the-route/triverroute.kml">...</a>
         ^
     delete this

Thank you all