Good afternoon guys,
I need to validate the maximum size of an attachment but my code is not working, can you help me?
Follow the code below:
HTML:
@using (Html.BeginForm("AdicionarAnexo", "Turma", FormMethod.Post, new { @encoding = "multipart/form-data", @enctype = "multipart/form-data", @id = "AnexoTurmaForm" }))
{
<input type="file" name="anexo" id="idAnexo" data-max-size="5" />
<input type="button" id="adicionarAnexo" value="Incluir anexo" onclick="teste()"/>
}
JAVASCRIPT:
function teste() {
var fileInput = $('#idAnexo');
var maxSize = fileInput.data('data-max-size');
$('#AnexoTurmaForm').button(function (e) {
if (fileInput.get(0).files.length) {
var fileSize = fileInput.get(0).files[0].size; // in bytes
if (fileSize > maxSize) {
alert('file size is more then' + maxSize + ' bytes');
return false;
} else {
alert('file size is correct- ' + fileSize + ' bytes');
}
} else {
alert('choose file, please');
return false;
}
});
};