mercredi 18 avril 2018

Alternative for fs.readFile when targeting the web using webpack

In my Electron application I am reading some application files (not user files, but actual files in the Electron application root) using fs.readFile however this obviously won't work when packing for the web. Alternatively I tried to implement something along the lines of the following:

function loadFile(filePath: string) {
    let result = null;
    const xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", filePath, false);
    xmlhttp.send();
    if (xmlhttp.status === 200) {
        result = xmlhttp.responseText;
    }
    return result;
}

Unfortunately I am getting the following exception: XMLHttpRequest cannot load file:///[...]/Testfile.txt. Cross origin requests are only supported for HTTP.

I'd like to know which approach I can use to load the content of a "server" file and not local file (even though I am testing locally at the moment) when packing for the web.




Aucun commentaire:

Enregistrer un commentaire