jquerycsvservletsjquery-csv

Read CSV from Jquery and post to Server


I'm writing Jquery for read CSV and convert to Array then send to Server (servlet) but its shows null. Please rectify it

in HTML

import jquery/1.8.2/jquery.min.js

import http://jquery-csv.googlecode.com/git/src/jquery.csv.js

input type "file"

button name "Click me"

script --

$(document).ready(function(){

$("button").click(function(event){

var files = $('#files').prop("files");

var reader = new FileReader();

var data = new Array();

reader.readAsText(files[0]);

reader.onload = function(event){

       var csv = event.target.result;

        data = $.csv.toArrays(csv);


       };

$.post("UploadCSVServlet",{arraydata:data,mode:"Insert"},

function(data) {

    alert("Data Loaded: " + data);

});

}); });

In Servlet :

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    System.out.println("enter upload servlet");
    resp.setContentType("text/plain");
    //resp.getWriter().println("Hello, world");
    String jsresponse="No Message";

       try
        {
        String arrayData[] = req.getParameterValues("arraydata[]");
        //String data[]=request.getParameterValues("arraydata[]");
        jsresponse = arrayData[0] +" - ";//"Test Response!";

        }
        catch(Exception ex)
        {
         jsresponse = ex.toString();               
        }
        resp.getWriter().write(jsresponse);
    }

It returns null values


Solution

  • var files = $('#files').prop("files");

    var reader = new FileReader();

    var data = new Array();

    reader.readAsText(files[0]);

    reader.onload = function(event){

       var csv = event.target.result;
    
        data = $.csv.toArrays(csv);
    

    $.post("UploadCSVServlet",{arraydata:data,mode:"Insert"},

    function(data) {

    alert("Data Loaded: " + data);
    

    });

    };

    }); });

    I use this. now its working

    just end bracket put after function(data).

    Thanks for all.