0

So I managed to get my file uploader from bootstrap to display the "path" of the file.

<div class="input-group">
    <div class="input-group-prepend">
        <span class="input-group-text" id="submitFile" onclick="upload()">Upload</span>
    </div>
    <div class="custom-file">
        <input type="file" class="custom-file-input" id="uploadFile"
               aria-describedby="inputGroupFileAddon01">
        <label class="custom-file-label" for="uploadFile">Choose file</label>
    </div>
</div>

My only problem is that I am confused with how to take that file and parse only the first column of values client side so my API call can read that information and perform database calls. Here is my script

<script>
function upload(){

    var csv = $("#uploadFile").val();
    console.log("CSV: " + csv);

    $.ajax({
        type: 'POST',
        url: endpoint + '/upload',
        dataType: 'json',
        data: //not sure how to load this,

        success: function(response){
            if(response === true)
                alert("Successfully")
            else
                alert("Invalid");
        }
    });
}
</script>

One of the ways that I could do this is to parse my CSV file and take the entries and place it into an array but I would be left with multiple columns worth of values. Other than that I am not sure how to process files client-side

Example CSV:

HEADER1,HEADER2,HEADER3
ONE, TWO,THREE
FOUR,FIVE,SIX
ONE,TWO,THREE

*EDIT So for clarity I want to do the following 1: Using the browser to open up a selected file and parse it to an array 2: Save that array as a json format for my api call

greatFritz
  • 165
  • 1
  • 14
  • 1
    I don't understand the question. Do you want to upload the file to the server, or process it client-side? – Barmar Aug 28 '19 at 15:48
  • 1
    See https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload/2320097#2320097 for how to upload a file using jQuery. – Barmar Aug 28 '19 at 15:49
  • 1
    See https://stackoverflow.com/questions/23331546/how-to-use-javascript-to-read-local-text-file-and-read-line-by-line for how to read a local text file client-side. – Barmar Aug 28 '19 at 15:50
  • @Barmar I am trying to read a CSV file client-side then save the data to a list to pass to api call – greatFritz Aug 28 '19 at 15:51

0 Answers0