如何在使用Dropzone.js和PHP上传文件后访问该文件

如何在使用Dropzone.js和PHP上传文件后访问该文件,php,ajax,dropzone.js,dropzone,Php,Ajax,Dropzone.js,Dropzone,我想在文件通过dropzone.js(v5)和PHP上传到服务器后访问它,也就是说,我想检索文件URL。我怎样才能得到它 form.php: <form action="upload.php" class="dropzone" enctype="multipart/form-data"> <div class="fallback"> <input name="file" type="file" multiple /> </

我想在文件通过dropzone.js(v5)和PHP上传到服务器后访问它,也就是说,我想检索文件URL。我怎样才能得到它

form.php:

<form action="upload.php" class="dropzone" enctype="multipart/form-data">
    <div class="fallback">
        <input name="file" type="file" multiple />
    </div>
</form>

<script src="/js/dropzone.js"></script>
<script>
    Dropzone.autoDiscover = false;
    $('.dropzone').dropzone({
        init: function () {
            this.on("complete", function (file) {
                runthisfunction();
            }) // end on complete
        }
    }); 
</script>

由于该文件是用PHP上传的,并在
upload.PHP
中重命名,因此我认为我需要以某种方式将该文件名“发布”到另一个文件中,然后将其检索回来。如何才能做到这一点?

您关于需要从服务器获取文件名的直觉是正确的。有一个例子说明了基本的想法

1) 您的服务器必须对上传帖子做出响应,并提供文件放置位置的详细信息,例如URL、文件名、路径等。因此,在您的PHP代码末尾,您需要执行以下操作:

// ... rest of your PHP ...
move_uploaded_file($tempFile,"images/$newname");

// Let the browser/JS know we are sending a JSON response back.  Make sure there 
// is no output before this.
header('Content-type: application/json');
echo json_encode([
    'status' => 'OK',  // Not required, but maybe useful
    'image'  => "images/$newname",
]);
// Maybe you have an <img class='thumbnail'> in your HTML, ready to show the upload
$('.thumbnail').attr('src', response.image).fadeIn();
2) 在JS中,您需要接受来自服务器的响应,并对其进行处理。Dropzone文档将显示以下内容:

文件已成功上载。获取服务器响应作为第二个参数

这听起来像是我们需要的。因此,将
complete
处理程序替换为
success
处理程序,并添加第二个参数:

this.on("success", function (file, response) {
    console.dir(response);
    // response.image will be the relative path to your uploaded image.
    // You could also use response.status to check everything went OK,
    // maybe show an error msg from the server if not.
});
我上面链接的Dropzone常见问题解答项目使用
.emit()
方法显示图像,我对此不熟悉,文档中似乎也没有描述。试试看,也许这很管用,适合你的需要。如果没有,您可以执行以下操作:

// ... rest of your PHP ...
move_uploaded_file($tempFile,"images/$newname");

// Let the browser/JS know we are sending a JSON response back.  Make sure there 
// is no output before this.
header('Content-type: application/json');
echo json_encode([
    'status' => 'OK',  // Not required, but maybe useful
    'image'  => "images/$newname",
]);
// Maybe you have an <img class='thumbnail'> in your HTML, ready to show the upload
$('.thumbnail').attr('src', response.image).fadeIn();

//也许您有一个直觉,认为需要从服务器上取回文件名是正确的。有一个例子说明了基本的想法

1) 您的服务器必须对上传帖子做出响应,并提供文件放置位置的详细信息,例如URL、文件名、路径等。因此,在您的PHP代码末尾,您需要执行以下操作:

// ... rest of your PHP ...
move_uploaded_file($tempFile,"images/$newname");

// Let the browser/JS know we are sending a JSON response back.  Make sure there 
// is no output before this.
header('Content-type: application/json');
echo json_encode([
    'status' => 'OK',  // Not required, but maybe useful
    'image'  => "images/$newname",
]);
// Maybe you have an <img class='thumbnail'> in your HTML, ready to show the upload
$('.thumbnail').attr('src', response.image).fadeIn();
2) 在JS中,您需要接受来自服务器的响应,并对其进行处理。Dropzone文档将显示以下内容:

文件已成功上载。获取服务器响应作为第二个参数

这听起来像是我们需要的。因此,将
complete
处理程序替换为
success
处理程序,并添加第二个参数:

this.on("success", function (file, response) {
    console.dir(response);
    // response.image will be the relative path to your uploaded image.
    // You could also use response.status to check everything went OK,
    // maybe show an error msg from the server if not.
});
我上面链接的Dropzone常见问题解答项目使用
.emit()
方法显示图像,我对此不熟悉,文档中似乎也没有描述。试试看,也许这很管用,适合你的需要。如果没有,您可以执行以下操作:

// ... rest of your PHP ...
move_uploaded_file($tempFile,"images/$newname");

// Let the browser/JS know we are sending a JSON response back.  Make sure there 
// is no output before this.
header('Content-type: application/json');
echo json_encode([
    'status' => 'OK',  // Not required, but maybe useful
    'image'  => "images/$newname",
]);
// Maybe you have an <img class='thumbnail'> in your HTML, ready to show the upload
$('.thumbnail').attr('src', response.image).fadeIn();

//可能您有一个PHP脚本,您的PHP脚本可能只需要在完整回调函数的
文件
参数中使用您希望能够访问的任何数据进行响应?您的PHP脚本可能只需要在完整回调函数的
文件
参数中使用您希望能够访问的任何数据进行响应功能?