从上传按钮和文件onchange事件导入Alasql [英] Alasql import from upload button vs file onchange event

查看:317
本文介绍了从上传按钮和文件onchange事件导入Alasql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何从文件输入将文件导入ALASQL。有关于如何执行此操作的文档,但我的客户希望在选择文件时按下加载按钮。

I am trying to figure out how to import a file into ALASQL from a file input. There is documentation on how to do this but my client wants to have to press a load button vs when choosing the file.

以下是ALASQL的文档:

Here is the documentation from ALASQL:

<script src="xlsx.core.min.js"></script>
<p>Select CSV file to read:&lt;/p>
<input id="readfile" type="file" onchange="loadFile(event)"/>
<script>
    function loadFile(event) {
        alasql('SELECT * FROM FILE(?,{headers:true})',[event],function(data){
            // Process data here
        });
     }
</script>

https://github.com/agershun/alasql/wiki/How%20to%20upload%20form%20for%20txt %20和%20xlsx%20in%20javascript

我的客户想要这样的东西:

My client wants something like this:

<script src="xlsx.core.min.js"></script>
<p>Select CSV file to read:&lt;/p>
<input id="readfile" type="file"/>
<button onclick="loadfile()">Load</button>
<script>
    function loadFile() {
        var file=document.getElementById('readfile').files[0]
        alasql('SELECT * FROM FILE(?,{headers:true})',[file],function(data){
            // Process data here
        });
     }
</script>

我已经尝试了各种方法来实现这一目标但到目前为止还没有任何工作。我尝试过的一些方法包括创建自定义jQuery事件和上面的例子。

I have tried various methods to achieve this but nothing has worked so far. Some of the methods I have tried include creating custom jQuery events and the above example.

我找到了一篇SF文章,询问类似的东西,但没有答案。
使用AlaSQL和JQuery加载CSV文件

I have found a SF article that asks something similar but was unanswered. Loading CSV file with AlaSQL and JQuery

谢谢

推荐答案

我想出的一个答案是拆分加载文件处理成两个函数,例如:

One answer I came up with is to split the loadfile process into two functions such as this:

<script src="xlsx.core.min.js"></script>
<p>Select CSV file to read:&lt;/p>
<input id="readfile" type="file" onchange="loadfile(event)"/>
<button onclick="loadfile2()">Load</button>
<script>
    var loadFileTempData=[];
    function loadFile() { //load data file into variable
        var file=document.getElementById('readfile').files[0]
        alasql('SELECT * FROM FILE(?,{headers:true})',[file],function(data){
            loadFileTempData=data;
        });
     }
     function loadFile2(){ //process data from variable
         var data=loadFileTempData;
         loadFileTempData=[];
         // Process data here
     }
</script>

这篇关于从上传按钮和文件onchange事件导入Alasql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆