javascript - 无法验证上传的图片类型
问题描述
我正在按照教程系列制作一个 Android 壁纸应用程序。为此,我必须设计和创建管理图像的管理区域。我对图像验证感到震惊。我在下面粘贴了我的整个代码。
<h1>Catogeries</h1>
<hr>
<div class="row">
<div class="col-lg-5">
<h1>Add new</h1>
<form class="form-group">
<div>
<label for="Catogery-name">Enter name</label>
<input type="text" class="form-control" id="Catogery-name">
<div class="invalid-feedback">Please enter a catogery name</div>
</div>
<div>
<label for="Catogery-description">Description</label>
<input type="text" class="form-control" id="Catogery-description">
<div class="invalid-feedback">Please enter the valid description</div>
</div>
<div>
<label for="Catogery-thumbnail">Thumbnail</label>
<input type="file" class="form-control" id="Catogery-thumbnail">
<div class="invalid-feedback">Please upload a valid image</div>
</div>
<div class="form-group" style="margin-top:3%">
<img src="#" id="selected-thumbnail" width="250px" height="150px">
</div>
<div class="form-group">
<div class="progress" style="margin-top:2%">
<div class="progress-bar" style="width:0%" id="upload-progress">0%</div>
</div>
</div>
<div class="btn btn-primary" type="button" id="save-catogery">Save</div>
</form>
<div id="result">
</div>
</div>
<div class="col-lg-7">
<h1>Saved Catogeries</h1>
</div>
</div>
<script>
var validImageTypes = ["image/gif", "image/jpeg", "image/png"];
$("#selected-thumbnail").hide();
function previewThumbnail(thumbnail) {
if (thumbnail.files && thumbnail.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$("#selected-thumbnail").attr('src', e.target.result);
$("#selected-thumbnail").fadeIn();
}
reader.readAsDataURL(thumbnail.files[0]);
}
}
$("#Catogery-thumbnail").change(function() {
previewThumbnail(this);
});
$("#save-catogery").click(function() {
var catName = $("#Catogery-name").val();
var desc = $("#Catogery-description").val();
var thumbnail = $("#Catogery-thumbnail").prop("files")[0];
if (!catName) {
$("#Catogery-name").addClass("is-invalid");
return;
}
if (!desc) {
$("#Catogery-description").addClass("is-invalid");
return;
}
if (thumbnail == null) {
$("#Catogery-thumbnail").addClass("is-invalid");
return;
}
if ($.inArray(thumbnail["type"], validImageTypes) < 0) {
$("#Catogery-thumbnail").addClass("is-invalid");
return;
}
});
</script>
解决方案
推荐阅读
- node.js - UnhandledPromiseRejectionWarning: ReferenceError: AbortController is not defined
- flutter - 在颤振中访问值
- dji-sdk - 将 WaypointV2Mission 添加到时间轴
- javascript - 如何使用 jquery 从输入中附加我的 ul?
- javascript - Sonarqube 抛出错误删除“isNaN”的此覆盖
- android-jetpack-compose - 如何在jetpack compose中检测键盘打开和关闭?
- c - 在 C 中声明字符串而不给出大小
- azure - 使用 quectel bg96 模块通过 mqtts 协议连接到 azure 设备配置服务时出错
- c# - 使用 GtkSharp (GTK#) 在 C# 中拖放
- javascript - 可以公开我的 Google/OAuth 密钥吗?