Javascript即时上传和解析文件 [英] Javascript upload and parse file on fly
本文介绍了Javascript即时上传和解析文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用
<input type="file">
,然后将其解析为javascript数组而不将文件保存在服务器端? 您能否建议任何插件使用csv(或任何其他)文件类型来做到这一点.
and then parse it to javascript array without saving the file on server-side? Could you advice any plugin to do it with csv (or any other) file type.
在不使用后端的情况下这样做是个好主意吗?
Is it good idea to do things such way without using back-end?
谢谢.
推荐答案
用于上传csv的Js代码,并在客户端进行解析,然后将其绘制到控制台中
Js code for upload csv and parse it client side and then plot them into console
function uploadDealcsv () {};
/*------ Method for read uploded csv file ------*/
uploadDealcsv.prototype.getCsv = function(e) {
let input = document.getElementById('dealCsv');
input.addEventListener('change', function() {
if (this.files && this.files[0]) {
var myFile = this.files[0];
var reader = new FileReader();
reader.addEventListener('load', function (e) {
let csvdata = e.target.result;
parseCsv.getParsecsvdata(csvdata); // calling function for parse csv data
});
reader.readAsBinaryString(myFile);
}
});
}
/*------- Method for parse csv data and display --------------*/
uploadDealcsv.prototype.getParsecsvdata = function(data) {
let parsedata = [];
let newLinebrk = data.split("\n");
for(let i = 0; i < newLinebrk.length; i++) {
parsedata.push(newLinebrk[i].split(","))
}
console.table(parsedata);
}
var parseCsv = new uploadDealcsv();
parseCsv.getCsv();
<input type="file" id="dealCsv"/>
这篇关于Javascript即时上传和解析文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文