1

I have an array of files that you receive files of an input type = file. I can not use the input because the user must have the ability to cancel a file. I need to pass this file array to my webservice in Java using ajax. How I spend my array using the method POST without using the form?

My big problem is the token and the header, the two need be dynamic and I deal in form, but in this case, I do not a form, I want to spend a var filearray declared in JavaScript. I cannot use a jQuery library file upload, because I need to show the files in other div after an user cancel a file and I have no idea how I show the files in other div because the library clean the input after the user chooses the files. I do not know where the library puts the files for me to handle them. "var filearray = []" is a global variable.

My HTML:

<input type="file" id="myFiles" multiple size="50" onchange="myFilesFunction()">

My JS:

function myFilesFunction(){
var x = document.getElementById("myFiles");
var contfilearray = 0;
for (var i = 0; i < x.files.length; i++) {
    filearray[contfilearray] = x.files[i];
    contfilearray ++;
}}

My Ajax:

function salvaanexoszip() {
var numeroticket = 12345;
var data = new FormData();
jQuery.each(jQuery(filearray)[0], function(i, file) {
    data.append('file-'+i, file);
});


$.ajax({
    url: 'URL',
    type: 'POST',
    cache: false,
    contentType: false,
    processData: false,
    data: {
        ticket: numeroticket,
        files: data,
    },
    success: function(data) {
        alert("OK");
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert("NOT OK");
    }
});}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously – epascarello Sep 16 '16 at 16:57
  • 1
    Just and advice...it's about code conventions...you need to choose between function names in camel case or lowercase...also you need to decide between using English names or Spanish names....that way you can accomplish a more maintainable code – Hackerman Sep 16 '16 at 16:59
  • Sorry my skills. I am new to programming . But just to report , I am Brazilian and these names are Portuguese and not Spanish names ... – matheus ferraz Sep 16 '16 at 17:27
  • @epascarello, the other post is very good, I followed the footsteps of the best answer, but still with the same error. Follows: Invalid message CSRF token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'. – matheus ferraz Sep 16 '16 at 17:38
  • Sounds like you need to add the CSRF token in your ajax request headers. Token can probably be retreived from cookies. – neurotik Sep 17 '16 at 15:31

0 Answers0