Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Reading in files using JS

edited July 2020 in OSWeb

Hi all,

I am wondering whether there is a way to load in a file (.txt, .cvs or .JSON) using javascript in the inline_javascript item. Whether the file is from a different server or just from the file pool does not matter for me.

Is this possible?

Thanks,

M

Comments

  • edited July 2020

    Hi @Michif ,

    This recipe from stackoverflow should allow you read text from a file through a URL. However, this file will probably need to be placed on the same server as the experiment, otherwise it will likely be blocked as a security measure. But maybe it's useful anyway.

    function getText(url){
        // read text from URL location
        var request = new XMLHttpRequest();
        request.open('GET', url, true);
        request.send(null);
        request.onreadystatechange = function () {
            if (request.readyState === 4 && request.status === 200) {
                var type = request.getResponseHeader('Content-Type');
                if (type.indexOf("text") !== 1) {
                    return request.responseText;
                }
            }
        }
    }
    
    vars.my_text = getText('http://www.puzzlers.org/pub/wordlists/pocket.txt')
    console.log(vars.my_text)
    

    I'm also thinking about a way to allow access to files in the file pool in OSWeb. But for now that's not possible!

    Cheers,

    Sebastiaan

Sign In or Register to comment.