Как сделать так, чтобы html-перетаскивание принимало только видео
Я использую этот HTML код для загрузки видео файлов на мой сайт.
<div class="container mt-5">
<div class="row d-flex justify-content-center">
<div class="col-md-6">
<form method="post" action="#" id="#">
{% csrf_token %}
<div class="form-group files">
<label class="d-flex justify-content-center">Upload Your File </label>
<input type="file" class="form-control" multiple="" accept="video/mp4,video/x-m4v,video/*">
</div>
<div class="row row-cols-auto">
<div class="col mx-auto my-2">
<button class="btn btn-primary" type="submit">Upload</button>
</div>
</div>
</form>
</div>
</div>
</div>
и этот CSS
.files input {
outline: 2px dashed #92b0b3;
outline-offset: -10px;
-webkit-transition: outline-offset .15s ease-in-out, background-color .15s linear;
transition: outline-offset .15s ease-in-out, background-color .15s linear;
padding: 120px 0px 85px 35%;
text-align: center !important;
margin: 0;
width: 100% !important;
}
.files input:focus{ outline: 2px dashed #92b0b3; outline-offset: -10px;
-webkit-transition: outline-offset .15s ease-in-out, background-color .15s
linear;
transition: outline-offset .15s ease-in-out, background-color .15s linear;
border:1px solid #92b0b3;
}
.files{ position:relative}
.files:after { pointer-events: none;
position: absolute;
top: 60px;
left: 0;
width: 50px;
right: 0;
height: 56px;
content: "";
background-image: url(https://image.flaticon.com/icons/png/128/109/109612.png);
display: block;
margin: 0 auto;
background-size: 100%;
background-repeat: no-repeat;
}
.color input{ background-color:#f1f1f1;}
.files:before {
position: absolute;
bottom: 10px;
left: 0; pointer-events: none;
width: 100%;
right: 0;
height: 57px;
content: " or drag it here. ";
display: block;
margin: 0 auto;
color: #2ea591;
font-weight: 600;
text-transform: capitalize;
text-align: center;
}
когда я нажимаю на ввод и выбираю файл, он позволяет мне выбрать только видео. Но я могу легко перетащить любой другой тип файла в поле ввода.
Есть ли способ предотвратить сброс в поле файлов других типов, кроме видео?
Спасибо.
Также я использую django в качестве бэкенда, если это имеет значение.
в вашем javascript используйте
замените 'file' на .mp4 или любой другой тип файла, который вы используете
if (item.kind === 'file') {
const file = item.getAsFile();
console.log(`… file[${i}].name = ${file.name}`);
}
});
} else {
// Use DataTransfer interface to access the file(s)
[...ev.dataTransfer.files].forEach((file, i) => {
console.log(`… file[${i}].name = ${file.name}`);
});
}
}