No form, in an Input selecting multiple images
@using (Html.BeginForm("Edit", "RoomType", FormMethod.Post, new { enctype = "multipart/form-data", onSubmit = "return ValidateImagesUpload()" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-5">
<input id="file-upload" name="file-upload" type="file" multiple accept="image/*">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Salvar" class="btn btn-primary" />
@Html.ActionLink("Voltar", "Edit", "Company", new { id = ApplicationManager.CompanyId }, new { @class = "btn btn-default" })
</div>
</div>
}
I've created validation in JavaScript
function ValidateImagesUpload(e) {
var fileValue = document.getElementById("file-upload").files;
var file;
var _URL = window.URL || window.webkitURL;
debugger;
for (var i = 0; i < fileValue.length; i++) {
if (file = fileValue[i]) {
//var img = new Image();
var img = document.createElement("img");
img.src = _URL.createObjectURL(file);
if (img.width > 150 || img.height > 150) {
var message = document.querySelector(".message-error");
message.innerHTML = "<p>Arquivo: " + file.name + " não respeita o limite de 150x150 px</p>";
message.removeAttribute("hidden");
return false;
}
}
}
return true;
}
In the onSubmit event of the form I am calling my validation, but it is not working, so I was able to analyze the "img" is not loading (or having time to load), because the debugger is used or even before the "IF" that I test the validation I put an alert (img.width) it brings the value "0", but it enters the condition of "IF".
Is there any way to expect to load the object of type Image?