0

I have a Jquery element of input type='file'.

$('#test').prop('files')[0];

I'd like to change the #test to a string.

var all_data    = new FormData();
$('input[type="file"]').each(function(index) { 
    var i           = index+1;
    var input_name  = '#test'+i;
    var file_data   = $(input_name).prop('files')[0]; //I think error start here

    alert(file_data);

    all_data.append('file', file_data);
});
alert(all_data);

But, it's not working.

HTML in PHP script:

$jml = 3;
for($i=1;$i<=$jml;$i++){ 
   echo "<input type='file' name='test".$i."' id='test".$i."' 
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Shota
  • 515
  • 3
  • 18

1 Answers1

0

Try this :

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>
    <title></title>
    <script src="/Scripts/jquery-1.10.2.js"></script> </head> <body>
    <input type="file" />

    <script>
        var all_data = new FormData();
        $(document).ready(function () {
            //alert();
            $('input[type="file"]').change(function (e) {
                var file = e.target.files[0];
                all_data.append('file', file);
            });
        });

    </script> </body> </html>
Bimal Das
  • 1,882
  • 5
  • 28
  • 53