gpt4 book ai didi

php - Dropzone.js - 显示服务器上的现有文件

转载 作者:可可西里 更新时间:2023-11-01 13:07:19 26 4
gpt4 key购买 nike

我目前使用的是 dropzone.js v3.10.2 我在显示已上传的现有文件时遇到问题。我非常擅长 php,但是我对 ajax 和 js 的了解有限

如果你能帮忙就太好了

提前致谢

索引.php

    <html>

<head>

<link href="css/dropzone.css" type="text/css" rel="stylesheet" />


<script src="ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script src="dropzone.min.js"></script>

<script>

Dropzone.options.myDropzone = {
init: function() {
thisDropzone = this;

$.get('upload.php', function(data) {


$.each(data, function(key,value){

var mockFile = { name: value.name, size: value.size };

thisDropzone.options.addedfile.call(thisDropzone, mockFile);

thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "uploads/"+value.name);

});

});
}
};
</script>

</head>

<body>


<form action="upload.php" class="dropzone" id="my-dropzone"></form>

</body>

上传.php

<?php
$ds = DIRECTORY_SEPARATOR;

$storeFolder = 'uploads';

if (!empty($_FILES)) {

$tempFile = $_FILES['file']['tmp_name'];

$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;

$targetFile = $targetPath. $_FILES['file']['name'];

move_uploaded_file($tempFile,$targetFile);

} else {
$result = array();

$files = scandir($storeFolder); //1
if ( false!==$files ) {
foreach ( $files as $file ) {
if ( '.'!=$file && '..'!=$file) { //2
$obj['name'] = $file;
$obj['size'] = filesize($storeFolder.$ds.$file);
$result[] = $obj;
}
}
}

header('Content-type: text/json'); //3
header('Content-type: application/json');
echo json_encode($result);
}
?>

附言。我知道 php 正在检索数据

提前致谢

达米安

最佳答案

我检查了代码(来自 starTutorial),它对我也不起作用(?)

我设法通过替换它来让它工作:

$.get('upload.php', function(data) {
$.each(data, function(key,value) {
var mockFile = { name: value.name, size: value.size };
thisDropzone.options.addedfile.call(thisDropzone, mockFile);
thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "uploads/"+value.name);
});
});

用这个:

$.getJSON('files/list-all', function(data) {
$.each(data, function(index, val) {
var mockFile = { name: val.name, size: val.size };
thisDropzone.options.addedfile.call(thisDropzone, mockFile);
thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "uploads/" + val.name);
});
});

归功于此答案:https://stackoverflow.com/a/5531883/984975

编辑:在 4.0 版中,现有文件的缩略图将与其中的提示栏一起显示。要解决此问题,请添加:

thisDropzone.emit("complete", mockFile);

关于php - Dropzone.js - 显示服务器上的现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24009298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com