单击文件输入上的取消时图像不会变空?
Image not getting empty when cancel is clicked on file input?
我正在使用 Croppie 插件来实现裁剪图像功能。当用户单击取消文件输入时,输入文本字段变为空并且图像不会变空。当用户单击文件输入上的取消按钮时,我也想删除图像。
DEMO
HTML
<form action="test-image.php" id="form" method="post">
<input type="file" id="upload" value="Choose a file">
<div id="upload-demo"></div>
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result">Send</a>
</form>
JS
$(document).ready(function() {
var $uploadCrop;
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$uploadCrop.croppie('bind', {
url: e.target.result
});
$('.upload-demo').addClass('ready');
}
reader.readAsDataURL(input.files[0]);
}
}
$uploadCrop = $('#upload-demo').croppie({
viewport: {
width: 200,
height: 200,
type: 'circle'
},
boundary: {
width: 300,
height: 300
}
});
$('#upload').on('change', function() {
readFile(this);
});
$('.upload-result').on('click', function(ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'original'
}).then(function(resp) {
$('#imagebase64').val(resp);
$('#form').submit();
});
});
});
这对你有帮助
$( document ).ready(function() {
var $uploadCrop;
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$uploadCrop.croppie('bind', {
url: e.target.result
});
$('.upload-demo').addClass('ready');
}
reader.readAsDataURL(input.files[0]);
}
}
$uploadCrop = $('#upload-demo').croppie({
viewport: {
width: 200,
height: 200,
type: 'circle'
},
boundary: {
width: 300,
height: 300
}
});
$('#upload').on('change', function () { readFile(this); });
$('.upload-result').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'original'
}).then(function (resp) {
$('#imagebase64').val(resp);
$('#form').submit();
});
});
$('.reset').on('click', function(){
$('input').val('');
$('.cr-viewport').html('');
$('.cr-image').attr('src','');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="test-image.php" id="form" method="post">
<input type="file" id="upload" value="Choose a file">
<div id="upload-demo"></div>
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result">Send</a>
</form>
<a class="reset">cancel</a>
好吧,这可能是一个典型的场景,因为您无法将 click event
绑定到文件上传的 cancel
按钮,因为 文件对话框的结果不会暴露给浏览器。所以我的建议是,将 onchange event
写入文件上传元素并检查 其值是否为空 。如果是,则在销毁后重置 croppie
实例。这是更新后的代码和 working Fiddle.
HTML
<form action="test-image.php" id="form" method="post">
<input type="file" id="upload" value="Choose a file" onchange="clearImage(this)" />
<div id="upload-demo"></div>
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result">Send</a>
</form>
JS
var $uploadCrop;
var opts = {
viewport: {
width: 200,
height: 200,
type: 'circle'
},
boundary: {
width: 300,
height: 300
}
};
//change function.
function clearImage(ctrl) {
if ($(ctrl).val() == "") {
$('#upload-demo').croppie('destroy');
$uploadCrop=$('#upload-demo').croppie(opts);
}
}
$(document).ready(function() {
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$uploadCrop.croppie('bind', {
url: e.target.result
});
$('.upload-demo').addClass('ready');
}
reader.readAsDataURL(input.files[0]);
}
}
$uploadCrop = $('#upload-demo').croppie(opts);
$('#upload').on('change', function() {
readFile(this);
});
$('.upload-result').on('click', function(ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'original'
}).then(function(resp) {
$('#imagebase64').val(resp);
$('#form').submit();
});
});
});
我知道这个 post 已经有了一个答案 selected 但是有一个更简单的方法来检测用户是否取消了文件 select 对话框 window .
在您的 onchange
函数中,您可以使用带有简单 if 语句的 FileReader
构造函数来捕获用户是否 select 编辑了文件。基本上,FileReader
构造函数允许您的 Web 应用程序使用 File 或 Blob 对象读取用户 select 的文件内容。
你所要做的就是调用一个简单的if
语句来检查上传的文件是否可以被FileReader
读取。在这种情况下,如果用户点击文件 select 对话框 window 上的取消按钮,则无法读取文件。
下面是如何实现它的示例。尽管下面的示例是在 JS 中,但它可以在 jQuery 函数中使用。
input.onchange = function() {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
if(reader) {
// A FILE WAS SELECTED.
} else {
// THE USER CANCELLED THE FILE SELECTION.
}
}
您可以阅读有关 FileReader Constructor 的更多信息,以了解有关其功能和浏览器支持的更多信息。
这是我能找到的解决此问题的最简单方法。我希望它能帮助其他遇到此问题的人。
我正在使用 Croppie 插件来实现裁剪图像功能。当用户单击取消文件输入时,输入文本字段变为空并且图像不会变空。当用户单击文件输入上的取消按钮时,我也想删除图像。
HTML
<form action="test-image.php" id="form" method="post">
<input type="file" id="upload" value="Choose a file">
<div id="upload-demo"></div>
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result">Send</a>
</form>
JS
$(document).ready(function() {
var $uploadCrop;
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$uploadCrop.croppie('bind', {
url: e.target.result
});
$('.upload-demo').addClass('ready');
}
reader.readAsDataURL(input.files[0]);
}
}
$uploadCrop = $('#upload-demo').croppie({
viewport: {
width: 200,
height: 200,
type: 'circle'
},
boundary: {
width: 300,
height: 300
}
});
$('#upload').on('change', function() {
readFile(this);
});
$('.upload-result').on('click', function(ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'original'
}).then(function(resp) {
$('#imagebase64').val(resp);
$('#form').submit();
});
});
});
这对你有帮助
$( document ).ready(function() {
var $uploadCrop;
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$uploadCrop.croppie('bind', {
url: e.target.result
});
$('.upload-demo').addClass('ready');
}
reader.readAsDataURL(input.files[0]);
}
}
$uploadCrop = $('#upload-demo').croppie({
viewport: {
width: 200,
height: 200,
type: 'circle'
},
boundary: {
width: 300,
height: 300
}
});
$('#upload').on('change', function () { readFile(this); });
$('.upload-result').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'original'
}).then(function (resp) {
$('#imagebase64').val(resp);
$('#form').submit();
});
});
$('.reset').on('click', function(){
$('input').val('');
$('.cr-viewport').html('');
$('.cr-image').attr('src','');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="test-image.php" id="form" method="post">
<input type="file" id="upload" value="Choose a file">
<div id="upload-demo"></div>
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result">Send</a>
</form>
<a class="reset">cancel</a>
好吧,这可能是一个典型的场景,因为您无法将 click event
绑定到文件上传的 cancel
按钮,因为 文件对话框的结果不会暴露给浏览器。所以我的建议是,将 onchange event
写入文件上传元素并检查 其值是否为空 。如果是,则在销毁后重置 croppie
实例。这是更新后的代码和 working Fiddle.
HTML
<form action="test-image.php" id="form" method="post">
<input type="file" id="upload" value="Choose a file" onchange="clearImage(this)" />
<div id="upload-demo"></div>
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result">Send</a>
</form>
JS
var $uploadCrop;
var opts = {
viewport: {
width: 200,
height: 200,
type: 'circle'
},
boundary: {
width: 300,
height: 300
}
};
//change function.
function clearImage(ctrl) {
if ($(ctrl).val() == "") {
$('#upload-demo').croppie('destroy');
$uploadCrop=$('#upload-demo').croppie(opts);
}
}
$(document).ready(function() {
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$uploadCrop.croppie('bind', {
url: e.target.result
});
$('.upload-demo').addClass('ready');
}
reader.readAsDataURL(input.files[0]);
}
}
$uploadCrop = $('#upload-demo').croppie(opts);
$('#upload').on('change', function() {
readFile(this);
});
$('.upload-result').on('click', function(ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'original'
}).then(function(resp) {
$('#imagebase64').val(resp);
$('#form').submit();
});
});
});
我知道这个 post 已经有了一个答案 selected 但是有一个更简单的方法来检测用户是否取消了文件 select 对话框 window .
在您的 onchange
函数中,您可以使用带有简单 if 语句的 FileReader
构造函数来捕获用户是否 select 编辑了文件。基本上,FileReader
构造函数允许您的 Web 应用程序使用 File 或 Blob 对象读取用户 select 的文件内容。
你所要做的就是调用一个简单的if
语句来检查上传的文件是否可以被FileReader
读取。在这种情况下,如果用户点击文件 select 对话框 window 上的取消按钮,则无法读取文件。
下面是如何实现它的示例。尽管下面的示例是在 JS 中,但它可以在 jQuery 函数中使用。
input.onchange = function() {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
if(reader) {
// A FILE WAS SELECTED.
} else {
// THE USER CANCELLED THE FILE SELECTION.
}
}
您可以阅读有关 FileReader Constructor 的更多信息,以了解有关其功能和浏览器支持的更多信息。
这是我能找到的解决此问题的最简单方法。我希望它能帮助其他遇到此问题的人。