Skip to content

Instantly share code, notes, and snippets.

@matveev-ra
Last active February 6, 2025 15:32
Show Gist options
  • Save matveev-ra/785afd38af94306b437c20e232d7ac88 to your computer and use it in GitHub Desktop.
Save matveev-ra/785afd38af94306b437c20e232d7ac88 to your computer and use it in GitHub Desktop.
[Добавление файла асинхронно] добавление файла в форме без привязки к id #C#
###кликаем по LABEL #bt_add_file for="input__file" мы добавляем файл в input с таким id у input
но кторый находится в скрытой форме с классом input.BtAddFile-js
<div class="file-wrapper">
<label for="input__file" class="input__file-button" attr-contractId='@Model.Id' attr-AdditionalAgreementId='0' id="bt_add_file">
<span class="input__file-icon-wrapper"><i class="fas fa-file-download"></i></span>
<span class="input__file-button-text">Добавить скан договора</span>
</label>
</div>
<form hidden method="post" id="hiddenFormForFile" class="hiddenForm" enctype="multipart/form-data">
<input type='hidden' class='idContractAddFiles-js' value='@Model.Id' name='id'>
<input id='input__file' name="file" accept=".pdf, .jpg, .jpeg, .png" type="file" class="input input__file BtAddFile-js">
</form>
#### оnслеживаем изменения у поля input.BtAddFile-js в скрытой формы #hiddenFormForFile
$('body').on('change', '.BtAddFile-js', function () {
event.preventDefault();
$('body #bt_add_file').css('display', 'none');
var contractId = $('body #bt_add_file').attr('attr-contractId');
SaveFormParcial()
.then(result => {
sendFileContract("/contract/FileAdd/").then(result =>
{
$.get("/contract/EditContract/", { id: contractId }).done(function (result)
{
$('.modal-window-content').html(result);
$('.modal-window-content').addClass('modal-big').removeClass('size-small');
showModal();
});
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment