简体   繁体   中英

how to read file from a path which is in text area in jquery

i want to read the file whose path comes dynamically and stores in the text area how to read that file from text area. so far i have made this but it still doesnot work.

Below is my code.

        google.load("visualization", "1", {packages:["corechart", "charteditor"]});
         $(function(){ 

            $("#csv").bind("change", function(event){               

                var reader = new FileReader();
                reader.onload = function(theFile) { 

                    try {
                        alert(theFile.target.result);

                        var input = $.csv.toArrays(theFile.target.result);

                    }
                    catch(e) { 
                        alert("CSV Parse error.");
                        return; 
                    }
                    $("#output").pivotUI( input, {
                        renderers: $.extend(
                        $.pivotUtilities.renderers, 
                        $.pivotUtilities.gchart_renderers, 
                        $.pivotUtilities.d3_renderers
                        )
                    });
                };
                reader.readAsText(event.target.files[0]);
            });  
         });

<% String d=request.getParameter("DP")!=null?request.getParameter("DP").toString():""; 
System.out.println(d);
%>
    <p>
    <input type="text" id="csv" value="<%=d%>"> 
    <div id="output" style="margin: 10px;">

    </div>

I don't think tou can read data from a file in jQuery / JavaScript directly.

A better way is to deal with the server side to provide data in JSON or something like that.

Check this site for more help about JSON: JSON Example

Here is the code for reading the text file using jquery:

var value='';
var pathOffile=''; ( here you can dynamically pass the path )

$.get(pathOffile, function( data ) {                                
    value=data;
    alert('reading text value: '+value);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM