javascript - Using btoa to parse xls from javascript to C#
问题描述
I have written a program that sends an excel file uploaded by the user from javascript to C#
where it is then formatted. The code works well for .xlsx
files; however, when I try to parse .xls
files, I receive the following error:
System.ArgumentNullException: 'Value cannot be null. Parameter name: s'.
After some further testing with breakpoints, I believe I have found the problem, but I cannot find a solution. The following code is the javascipt used to encode the file:
document.getElementById('selectedFile').addEventListener('change', function (event) {
var reader = new FileReader();
reader.onload = function () {
filename = event.target.files[0].name;
var uint8Array = new Uint8Array(this.result);
fileContent = btoa(String.fromCharCode.apply(null, uint8Array));
}
reader.readAsArrayBuffer(this.files[0]);
}, false);
When the file uploaded is of type .xlsx
, fileContent
returns the correct value; however, if an .xls
file is uploaded, it returns null
, therefore breaking the code further on.
Is there a way to make this code work for .xls
files? Alternatively, is there a way I can convert it to .xlsx
before I parse it to the backend?
解决方案
.xlsx
是一种文本格式 (XML),因此String.fromCharCode
应该可以正常工作。
.xls
是一种二进制文件格式。我不希望 String 函数可以正常使用它。
您可以使用 Excel 在格式之间进行转换。转换是不平凡的,虽然不是不可能从 javascript 完成,但可能不值得花几个月的时间。
推荐阅读
- android - Nativescript / Android:avdmanager 显示设备,但 nativescript 找不到它们
- python - Tkinter 消息框在解雇后重新出现
- r - 确定与 R 中的多边形相交的线
- java - java Regex:如何找到前面没有空格和逗号的实数
- c++ - 为一个点分配内存(枚举)
- ios - 如何在不使用 Cocoapods 的情况下注册权限处理程序?
- java - 如何为在 Android 布局上动态添加的视图赋予权重?
- github - 有没有
Visual Studio 代码中的按钮 - python-3.x - 将从 Stack Exchange API 检索到的日期转换为某种可读格式
- javascript - Vuelidate 如何用于限制文本输入的最小长度?