mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
44 lines
1.1 KiB
4 years ago
|
// Ref : https://stackoverflow.com/a/12002275
|
||
|
|
||
3 years ago
|
// Tested in Mozilla Firefox browser, Chrome
|
||
3 years ago
|
function ReadFileAllBrowsers(FileElement, CallBackFunction) {
|
||
4 years ago
|
try {
|
||
3 years ago
|
if (!FileElement.files || !FileElement.files.length) { return CallBackFunction() }
|
||
4 years ago
|
|
||
3 years ago
|
const file = FileElement.files[0]
|
||
4 years ago
|
|
||
|
if (file) {
|
||
3 years ago
|
const reader = new FileReader()
|
||
|
reader.readAsText(file, 'UTF-8')
|
||
3 years ago
|
reader.onload = function(evt) {
|
||
3 years ago
|
CallBackFunction(evt.target.result)
|
||
4 years ago
|
}
|
||
3 years ago
|
reader.onerror = function(evt) {
|
||
4 years ago
|
CallBackFunction()
|
||
|
}
|
||
|
}
|
||
|
} catch (Exception) {
|
||
3 years ago
|
const fallBack = ieReadFile(FileElement.value)
|
||
|
// eslint-disable-next-line eqeqeq
|
||
|
if (fallBack != false) {
|
||
|
CallBackFunction(fallBack)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/// Reading files with Internet Explorer
|
||
3 years ago
|
function ieReadFile(filename) {
|
||
4 years ago
|
try {
|
||
3 years ago
|
// eslint-disable-next-line no-undef
|
||
|
const fso = new ActiveXObject('Scripting.FileSystemObject')
|
||
|
const fh = fso.OpenTextFile(filename, 1)
|
||
|
const contents = fh.ReadAll()
|
||
|
fh.Close()
|
||
|
return contents
|
||
4 years ago
|
} catch (Exception) {
|
||
3 years ago
|
return false
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
export default ReadFileAllBrowsers
|