mardi 11 octobre 2016

Using pasted string or file upload

I have a web form where a user is uploading between one and two files. Each file can be given through a file input or by pasting text into a text box.

How can I standardize the input so that it is sent to the server in the same fashion regardless of the method used to input? I want to send the server one type of data always, either a multipart request with a file or a JSON request with the file as a string. How?

Right now, I'm trying to read files using a FileReader, but I am confused about handling asynchronous reads.

function getPrimaryFileString() {
    if(pasted()) {
        return document.getElementById("pastedPrimaryInputFile").value;
    } else {
        var reader = new FileReader();
        var file = document.getElementById("primaryInputFile").files[0];
        reader.readAsText(file);

        filestring = reader.contents;

        return filestring;
    }
}

I know that I am supposed to have an onload callback, but I don't know how I would do that and be able to return the filestring like this.




Aucun commentaire:

Enregistrer un commentaire