javascript - 如何检查此 dropzone.js 引导示例中是否已添加文件?
问题描述
输入文件类型已被样式化为按钮。我不明白的是如何获取文件输入以检查它是否已添加。
拖放区配置
var myDropzone = new Dropzone(".container", { // Make the whole body a dropzone
url: "/images-save", // Set the url
thumbnailWidth: 80,
thumbnailHeight: 80,
parallelUploads: 20,
previewTemplate: previewTemplate,
maxFilesize: 255,
maxFiles: 1,
autoQueue: false, // Make sure the files aren't queued until manually added
previewsContainer: "#previews", // Define the container to display the previews
clickable: ".fileinput-button", // Define the element that should be used as click trigger to select files.
acceptedFiles: ".png,.jpg,.gif,.jpeg",
dictFileTooBig:"image file exceeds ----",
autoProcessQueue: false,
});
Dropzone.js 引导程序
<div id="actions" class="row">
<div class="col-lg-7">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button dz-clickable">
<i class="glyphicon glyphicon-plus"></i>
<span>Add files...</span>
</span>
<button type="submit" class="btn btn-primary start" >
<i class="glyphicon glyphicon-upload"></i>
<span>Start upload</span>
</button>
<button type="reset" class="btn btn-warning cancel">
<i class="glyphicon glyphicon-ban-circle"></i>
<span>Cancel upload</span>
</button>
</div>
</div>
例如,一个普通的输入表单是这样的:
<input type="file" id="inputfile">
我会这样做来检查:
if(document.getElementById("inputfile").value != "") {
// checking if there is a file added
}
那么如何使用 dropzone.js 引导程序来做到这一点?
解决方案
Dropzone 有一个名为“ addedFile”的事件
您可以在本文档中阅读有关它支持的事件的更多信息。
// The recommended way from within the init configuration:
Dropzone.options.myAwesomeDropzone = {
init: function() {
this.on("addedfile", function(file) { alert("Added file."); });
}
};
此代码可能对您有用:
var myDropzone = new Dropzone(".container", { // Make the whole body a dropzone
url: "/images-save", // Set the url
thumbnailWidth: 80,
thumbnailHeight: 80,
parallelUploads: 20,
previewTemplate: previewTemplate,
maxFilesize: 255,
maxFiles: 1,
autoQueue: false, // Make sure the files aren't queued until manually added
previewsContainer: "#previews", // Define the container to display the previews
clickable: ".fileinput-button", // Define the element that should be used as click trigger to select files.
acceptedFiles: ".png,.jpg,.gif,.jpeg",
dictFileTooBig:"image file exceeds ----",
autoProcessQueue: false,
init: function() {
this.on("addedfile", function(file) { alert("Added file."); });
}
});
推荐阅读
- java - Android 10+ 未从 MediaStore 检索 DATE_TAKEN 信息
- spring-boot - 从实现 jar 中排除 spring-cloud-deployer-spi-scheduler-test-app
- python - 在 Alembic 迁移文件头中包含用户名
- web-scraping - 有没有办法获取抽搐视频数据?
- java - ArrayList 索引超出范围但 HashSet 没有问题
- reactjs - 如何使用道具更新我的表单 Reactjs
- c++ - 创建一个接收混杂数据包的套接字
- scala - 如何构建需要多个 Scala 版本的项目?
- android - 如何在 Android 上调查墓碑崩溃数据
- javascript - 未输入国家/地区时的 Covid-19 错误消息与 API 不匹配