How to make the browser load data created by the browser?

there is a browser program that allows the user to play chess - move pieces, etc., trying to allow the user to load the resulting pgn (Content-Type: PGN) directly from the browser.

Does it have anything to do with data: URI? is there any example somewhere?

only interested in modern browsers

+3
source share
2 answers

You can use data URIs, but there are some limitations. Here is an example based on my answer to an earlier question . The first thing you will notice is that you cannot control the file name, but it works fine in Firefox and Chrome, except for that, but maybe not so well in IE (I haven't tried it).

Assuming you can already generate PGN as a string, the code to create the data URI is pretty simple:

function exportData(data, target) {
    var exportLink = document.createElement('a');
    exportLink.setAttribute('href', 'data:application/x-chess-pgn;base64,' + window.btoa(data));
    exportLink.appendChild(document.createTextNode('sample.pgn'));
    document.getElementById(target).appendChild(exportLink);
}

Just set the data with what you are creating and configure the element to hold the link after it is created.

In the future, we will have better solutions for these kinds of problems, but there is no browser support yet.

0
source

, . , PNG, , ?

, , appriopriate MIME- HTTP- " ".

PHP :

header("Content-Type: application/force-download"); 

header("Content-Type: application/octet-stream");

MIME, .

+2

All Articles