mirror of
https://github.com/huashengdun/webssh.git
synced 2026-08-20 04:36:42 +00:00
Defined a function read_file_as_text
This commit is contained in:
+39
-19
@@ -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() {
|
||||
var name;
|
||||
|
||||
@@ -112,6 +140,16 @@ jQuery(function($){
|
||||
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) {
|
||||
// for console use
|
||||
if (new_encoding === undefined) {
|
||||
@@ -211,25 +249,7 @@ jQuery(function($){
|
||||
};
|
||||
|
||||
sock.onmessage = function(msg) {
|
||||
var reader = new window.FileReader();
|
||||
|
||||
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);
|
||||
read_file_as_text(msg.data, term_write, decoder);
|
||||
};
|
||||
|
||||
sock.onerror = function(e) {
|
||||
|
||||
Reference in New Issue
Block a user