Commit 6cfcadda by Sheng

Defined a function read_file_as_text

parent 4d11a2ce
...@@ -66,6 +66,34 @@ jQuery(function($){ ...@@ -66,6 +66,34 @@ jQuery(function($){
} }
function read_file_as_text(file, callback, decoder) {
var reader = new window.FileReader();
if (decoder === undefined) {
decoder = new window.TextDecoder('utf-8', {'fatal': true});
}
reader.onload = function () {
var text;
try {
text = decoder.decode(reader.result);
} catch (TypeError) {
console.error('Decoding error happened.');
} finally {
if (callback) {
callback(text);
}
}
};
reader.onerror = function (e) {
console.error(e);
};
reader.readAsArrayBuffer(file);
}
function reset_wssh() { function reset_wssh() {
var name; var name;
...@@ -112,6 +140,16 @@ jQuery(function($){ ...@@ -112,6 +140,16 @@ jQuery(function($){
term.on_resize(geometry.cols, geometry.rows); term.on_resize(geometry.cols, geometry.rows);
} }
function term_write(text) {
if (term) {
term.write(text);
if (!term.resized) {
resize_terminal(term);
term.resized = true;
}
}
}
function set_encoding(new_encoding) { function set_encoding(new_encoding) {
// for console use // for console use
if (new_encoding === undefined) { if (new_encoding === undefined) {
...@@ -211,25 +249,7 @@ jQuery(function($){ ...@@ -211,25 +249,7 @@ jQuery(function($){
}; };
sock.onmessage = function(msg) { sock.onmessage = function(msg) {
var reader = new window.FileReader(); read_file_as_text(msg.data, term_write, decoder);
reader.onload = function() {
var text = decoder.decode(reader.result);
// console.log(text);
if (term) {
term.write(text);
if (!term.resized) {
resize_terminal(term);
term.resized = true;
}
}
};
reader.onerror = function(e) {
console.error(e);
};
reader.readAsArrayBuffer(msg.data);
}; };
sock.onerror = function(e) { sock.onerror = function(e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment