-1

I tried some method for this but I couldn't do it somehow. I have a openfile dialog for choose .csv file and I've a button for start indexing data. My HTML codes like this

    <body>
    <div style="padding-left: 5%; padding-right: 5%; padding-top: 25px">
</div>
        <div id="wrapper" style="width: 100%; text-align: left">
            <img border="0" src="images//TEC.jpg" alt="TEC" width="170"
                height="75">
        <font size="6"> Electronic Archiving</font>
        </div>
        <br>
        <br>
        <br>
        <form id="indexForm" method="post" enctype="multipart/form-data">
        <div id="update" style="width:100%; text-align:left; margin-left:2.5em">
        <label for="updateFile">File:&nbsp;</label><input type="file" id="updateFile" style="position:fixed" accept=".csv">
        <br>
        <br>
        <div id="buton" style="margin-left:5em"><input type="button" value="UPDATE FILE" class="btn btn-sm" id="indexCSV"></div>
        </div>
        </form>
</body>

And my jquery like this

    <script>
$(document).ready(function(){
    $("#indexCSV").click(function(){
        var file=document.getElementById("updateFile").files[0];
        var formData=new FormData();
        formData.append('file',file);
        $.ajax({
            type:'POST',
            url:'http://localhost:8983/archiveCore/update?commit=true',
            dataType:'csv'
            contentType:'text/csv',
            processData:false\
            data:'formData',
            success:function(){
                window.alert("Data uploaded : ")
            }
        });
    });
});
</script>

When I'm clicking the button there is no action. I don't understand why.

  • Any errors in your console? Is the `updateFile` function triggered but the ajax post is not sent? Do you see the post in the network tab? – empiric Mar 18 '19 at 09:02
  • There is no error in my console. Yes, I guess the ajax post is not sent the file. – Mustafa Berkan Demir Mar 18 '19 at 09:11
  • You've asked this same question, with the exact same code, on at least 4 separate occasions now, and every time I've closed it as a duplicate. Make the effort to read the duplicate; you need to put the binary file data in to a `FormData` object and send that in the AJAX request. – Rory McCrossan Mar 18 '19 at 10:19
  • I tried it so many times but it's not working. @RoryMcCrossan – Mustafa Berkan Demir Mar 18 '19 at 11:14
  • Then please show what you've changed in the logic you tried. Every question you've asked has shown identical code with no changes made to fix the problem. – Rory McCrossan Mar 18 '19 at 11:20
  • Thank you. I've added an answer for you, although all this is really contained in the question I've linked to multiple times. All you had to do was copy+paste it. – Rory McCrossan Mar 18 '19 at 13:53

1 Answers1

1

There's several issues in your logic. Firstly you need to pass formData as a reference to the variable, not a string. You also have a syntax error after processData, it should be a , not a /. The value of that property should also be false in this case.

Finally the dataType should be changed from text/csv to whatever you're expecting the server to return, most commonly JSON or HTML. You could omit this property completely and let jQuery figure out the response format itself.

Here's an updated version:

$("#indexCSV").click(function() {
  var file = $("updateFile")[0].files[0];
  var formData = new FormData();
  formData.append('file', file);

  $.ajax({
    type: 'POST',
    url: 'http://localhost:8983/archiveCore/update?commit=true',
    contentType: false,
    processData: false,
    data: formData,
    success: function() {
      window.alert("Data uploaded : ")
    }
  });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • I did it your way but unfortunately nothing changed. My example Solr cURL like that --> curl 'http://localhost:8983/solr/archiveCore/update?commit=true' --data-binary @'FileDirectory' -H 'Content-type:application/csv' – Mustafa Berkan Demir Mar 18 '19 at 14:04
  • Can you please give more information than 'nothing changed'. Is the request actually sent? What's the response from the server? I'm trying my hardest to help you here, but you're really not making it easy – Rory McCrossan Mar 18 '19 at 14:09
  • I understand you man my english vocabulary is very bad but I'll do my best to explain you what I want and what is the problem. So I'm choosing my .csv file to index Solr core and I'm clicking update button but nothing happen. I added a error message but there is not any success or error message when I'm clicking update button. I tried many method to fix this problem but I couldn't do it yet. – Mustafa Berkan Demir Mar 18 '19 at 14:17