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.

appleman
  • 150
  • 1
  • 10
  • I would use AJAX to read `.dat` files. Look here: http://stackoverflow.com/questions/13623784/read-txt-file-real-time-with-javascript note it does not matter whether it's `.txt` or `.dat`. – Spencer Wieczorek Aug 22 '14 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? – appleman Aug 22 '14 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() – dandavis Aug 22 '14 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... – appleman Aug 22 '14 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. – dandavis Aug 22 '14 at 20:13
  • Ah I see, ok. I will try so manipulate this data in to the correct form that I need. Thank you for the suggestions. I will look in to that. – appleman Aug 22 '14 at 20:19
  • @user5465 This is because AJAX can simply read the text, not interpret a `.dat` that has binary information. You will have to functionally interpret binary information. Look here: http://stackoverflow.com/questions/327685/is-there-a-way-to-read-binary-data-in-javascript Sorry, should of mentioned that In my last comment. – Spencer Wieczorek Aug 22 '14 at 23:58

1 Answers1

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);
    });
});
mitramir
  • 11
  • 2