2
In the form, in an Input by 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 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 if I use Debugger or even before the "IF" that tests the validation I put an Alert(img.width) it carries the value "0", but enters the condition of the "IF". Is there any way to expect to load the Image type object?
Thanks for the explanation, I’m starting to mess with the Web these last 3 weeks, I will perform some tests and research on the subject
– Lucas Riechelmann Ramos