In my $scope.accept I'm trying to upload some files to my server,
the script is working when I just do <form id="mForm" action="http://.php" method="post" enctype="multipart/form-data">
in my HTML.
But I want to stop the redirect, so I want to do it with ajax on my controller. When I run the same script in the controller side I get this error: TypeError: Cannot read property 'length' of undefined at subir ..... At this line particularly: var i = 0, len = filedata.files.length, img, reader, fil;
function subir() {
alert("ok");
var filedata = document.getElementsByName("file"),
formdata = false;
if (document.FormData) {
formdata = new FormData();
}
var i = 0, len = filedata.files.length, img, reader, file;
for (; i < len; i++) {
file = filedata.files[i];
if (document.FileReader) {
reader = new FileReader();
reader.onloadend = function(e) {
showUploadedItem(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("file", file);
}
}
if (formdata) {
$.ajax({
url: "http:.php",
type: "POST",
dataType:'json',
data: {json: formdata},
processData: false,
contentType: false,
success: function(res) {
alert("success");
},
error: function(res) {
alert("error");
}
});
}
};
This is part of the HTML:
<input type="file" accept="image/*;capture=camera" onchange="openFile_1(event)" id="file_1" class="custom-file-input" name="file[]">
<img id="srcImage1" height="0">
</span>
<span class="item item-input">
<input type="text" id="obs_1" placeholder="Observaciones" style="text-align:left;"/>
</span>
<span class="item item-input">
<input type="file" accept="image/*;capture=camera" onchange="openFile_2(event)" id="file_2" class="custom-file-input" name="file[]">