【发布时间】:2020-05-20 17:13:44
【问题描述】:
我正在使用 krajee 文件输入将图像上传到服务器。我还希望能够使用屏幕截图添加文件。这个想法是,当我捕获图像时,它会出现在文件上传预览区域中。
我的html
<input th:field="*{documents}" class="file" id="input-res-capture" type="file" multiple>
<div class="col-md-4">
<video id="videostream" autoplay></video>
<p><button class="btn btn-primary capture-button">Start video</button>
<p><button class="btn btn-primary" id="screenshot-button" disabled>Take screenshot</button></p>
<p><button class="btn btn-primary" id="stop-button">Stop</button></p>
</div>
<div class="col-md-4">
<img id="screenshot-img">
</div>
我的 Krajee 文件上传插件代码
$("#input-res-capture").fileinput({
browseOnZoneClick: false,
showUpload: false,
showCancel: false,
maxFileCount: 5,
allowedFileExtensions: ['pdf', 'png', 'jpeg', 'jpg'],
showPreview: true,
removeLabel: 'Remove All',
mainClass: "input-group-lg",
theme: "fa",
showBrowse: false,
dropZoneTitle: 'Captured images will appear here',
initialPreviewAsData: false,
fileActionSettings: {
showRemove: true,
showUpload: false,
showZoom: true,
showDrag: false,
showCancel: false,
},
});
我的屏幕截图代码
const constraints = {
video: true
};
const captureVideoButton = document.querySelector('#screenshot .capture-button');
const screenshotButton = document.querySelector('#screenshot-button');
const stopButton = document.querySelector('#stop-button');
const fileInput = document.querySelector('#input-res-capture');
const img = document.querySelector('#screenshot img');
const video = document.querySelector('#screenshot video');
const canvas = document.createElement('canvas');
captureVideoButton.onclick = function(event) {
event.preventDefault()
navigator.mediaDevices.getUserMedia(constraints).
then(handleSuccess).catch(handleError);
};
screenshotButton.onclick = video.onclick = function(event) {
event.preventDefault()
canvas.width = 200;
canvas.height = 300;
canvas.getContext('2d').drawImage(video, 0, 0);
img.src = canvas.toDataURL('image/webp');
}
非常感谢任何帮助
【问题讨论】:
标签: javascript html file input bootstrap-4