2

I am trying to upload a file example.dat using javascript. I thought the right path was using fileReader, but it seems that it is unable to handle this format.

The goal is: importing this .dat file on my side to upload a list of words, in binary, and then after the import, translating them back in to words as the final product. This method is used to save space as I am creating a game where space is limited.

I looked in to DataView but I am having trouble resolving how to import a .dat file and read the resulting import.

Thank you in advanced.

7
  • I would use AJAX to read .dat files. Look here: stackoverflow.com/questions/13623784/… note it does not matter whether it's .txt or .dat. Commented Aug 22, 2014 at 19:01
  • I used this method just now and when I use .dat files, it doesn't seem to load. Maybe, the dataType need to be different? Commented Aug 22, 2014 at 19:34
  • FileReader can handle any format. for ajax, just set xhr.responseType="blob"; and then you can feed the response to FileReader, just like when you have a File() Commented Aug 22, 2014 at 20:02
  • @dandavis, I was using the template that Spencer suggested I look at. It imported .txt files fine, but not .dat... So, I have this binary file and I want to read it and convert it back in to words, as a way of saving space. I feel like it isn't interpreting the data correctly, this binary file has other characters other than 1's and 0's... Commented Aug 22, 2014 at 20:10
  • getting the blob from an input or via ajax is no problem, it's what you're doing with the blob once you get it that matters. there is no standard dat file, so i can't tell you how to turn those bits into strings, but reading is as text clearly doesn't work. maybe you want a UInt8Array or to convert the char values into unicode chars. Commented Aug 22, 2014 at 20:13

1 Answer 1

1

I have an issue just like yours and I use these code to read required data from .dat file:

    $("#importFileBtn").change(function (event) {
    $.each(event.target.files, function (index, file) {
        var reader = new FileReader();
        reader.onload = function (e) {
            // e.target.result should contain the text
            log(e.target.result);
        };
        reader.readAsText(file);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.