如何从 Dropzone 的下拉框区域删除文件?
How to remove a file from the drop box area of Dropzone?
我使用 Dropzone.js 上传文件 application.After 用户从他本地计算机的文件管理器中选择图片到 dropzone 区域,如果用户后来发现 he/she想删除其中一张挑选的图片或files.How来实现吗?
HTML代码:
<form id="file-up" action="upload_file.php" method="post" enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
<div class="fallback">
<input type="file" id="upload_file" name="upload_file[]" multiple/>
<input type='hidden' id='uploadvalues' />
</div>
</form>
jquery
<script type="text/javascript">
Dropzone.options.myAwesomeDropzone = {
maxFilesize: 20, // Size in MB
addRemoveLinks: true,
removedfile: function(file) {
var fileRef;
return (fileRef = file.previewElement) != null ?
fileRef.parentNode.removeChild(file.previewElement) : void 0;
},
success: function(file, response) {
alert(response);
},
error: function(file, response) {
alert(response);
}
};
</script>
PHP 用于上传的服务器端代码
<?php
if(isset($_POST['submit_image'])){
$ds = DIRECTORY_SEPARATOR; //1
$storeFolder = 'uploads'; //2
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; //3
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //4
$targetFile = $targetPath. $_FILES['file']['name']; //5
move_uploaded_file($tempFile,$targetFile); //6
}
}
?>
您可以像这样创建一个 delete.php 文件。
</p>
include 'db.php';
$upload_dir = 'myuploads';
$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $upload_dir . DIRECTORY_SEPARATOR;
unlink($targetPath.$_GET['fid']);
$obj=new DB();
$sql = "DELETE FROM file_upload WHERE f_name='".$_GET['fid']."'";
$retval = mysqli_query($obj->connection(),$sql);
print_r("Successfully deleted.");
<p style="text-align: justify;">
更新自评论:如果我理解正确,你希望 X 删除 dropzone 中的文件。
removedfile: function(file) {
x = confirm('Do you want to delete?');
if(!x) return false;
}
您还可以使用以下代码段:
Dropzone.autoDiscover = false;
$(".dropzone").dropzone({
addRemoveLinks: true,
removedfile: function(file) {
var name = file.name;
$.ajax({
type: 'POST',
url: 'upload.php',
data: {name: name,request: 2},
sucess: function(data){
console.log('success: ' + data);
}
});
var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
}
})
我使用 Dropzone.js 上传文件 application.After 用户从他本地计算机的文件管理器中选择图片到 dropzone 区域,如果用户后来发现 he/she想删除其中一张挑选的图片或files.How来实现吗?
HTML代码:
<form id="file-up" action="upload_file.php" method="post" enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
<div class="fallback">
<input type="file" id="upload_file" name="upload_file[]" multiple/>
<input type='hidden' id='uploadvalues' />
</div>
</form>
jquery
<script type="text/javascript">
Dropzone.options.myAwesomeDropzone = {
maxFilesize: 20, // Size in MB
addRemoveLinks: true,
removedfile: function(file) {
var fileRef;
return (fileRef = file.previewElement) != null ?
fileRef.parentNode.removeChild(file.previewElement) : void 0;
},
success: function(file, response) {
alert(response);
},
error: function(file, response) {
alert(response);
}
};
</script>
PHP 用于上传的服务器端代码
<?php
if(isset($_POST['submit_image'])){
$ds = DIRECTORY_SEPARATOR; //1
$storeFolder = 'uploads'; //2
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; //3
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //4
$targetFile = $targetPath. $_FILES['file']['name']; //5
move_uploaded_file($tempFile,$targetFile); //6
}
}
?>
您可以像这样创建一个 delete.php 文件。
</p>
include 'db.php';
$upload_dir = 'myuploads';
$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $upload_dir . DIRECTORY_SEPARATOR;
unlink($targetPath.$_GET['fid']);
$obj=new DB();
$sql = "DELETE FROM file_upload WHERE f_name='".$_GET['fid']."'";
$retval = mysqli_query($obj->connection(),$sql);
print_r("Successfully deleted.");
<p style="text-align: justify;">
更新自评论:如果我理解正确,你希望 X 删除 dropzone 中的文件。
removedfile: function(file) {
x = confirm('Do you want to delete?');
if(!x) return false;
}
您还可以使用以下代码段:
Dropzone.autoDiscover = false;
$(".dropzone").dropzone({
addRemoveLinks: true,
removedfile: function(file) {
var name = file.name;
$.ajax({
type: 'POST',
url: 'upload.php',
data: {name: name,request: 2},
sucess: function(data){
console.log('success: ' + data);
}
});
var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
}
})