【发布时间】:2021-11-07 22:56:32
【问题描述】:
我正在尝试修复 Canvas Resize (Downscale) Image,我得到了 jsfiddle http://jsfiddle.net/EWupT/ 来调整图像大小。当用户上传即时图像显示在输入字段上时,我有 html 输入字段,当在我的现有代码上添加调整大小代码时出现错误。非常感谢任何帮助。
我的 JS:
$(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width=300;
canvas.height=234;
ctx.drawImage(reader, 0, 0, 300, 234);
$('.profile-pic').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$(".file-upload").on('change', function(){
readURL(this);
});
$(".upload-button").on('click', function() {
$(".file-upload").click();
});
});
我的 HTML:
<div class="upload-button" id="imageresize"><img class="profile-pic" src="../images/add-image.png" /></div>
<input id="formFile" id="avatar-2" class="file-upload" type="file" name="my_file" accept="image/*">
我收到了这个错误:
Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or OffscreenCanvas or SVGImageElement or VideoFrame)'.
at FileReader.reader.onload
【问题讨论】:
标签: javascript file-upload html5-canvas filereader