php - Dropzone.js 用 php 删除按钮

标签 php jquery drag-and-drop dropzone.js

我使用 dropzone.js 作为一个不错的上传表单。 我链接了 php 代码来上传文件,我设置了 addRemoveLinks=true 所以我有删除按钮。

当我点击删除按钮时,我需要知道如何有效地删除使用 PHP 代码上传的文件。

PHP 很简单,但我不需要知道如何将它们关联起来。 我已经尝试在此函数中使用 $.post removedfile: function(file) 但没有成功。

  removedfile: function(file) {
    $.post("test.php");
    var _ref;
    return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;

},

最佳答案

首先,您不应该简单地覆盖默认的 removedfile 事件处理程序,而应该同时注册您自己的处理程序。

您需要先从服务器取回 ID(这样您就知道如何关联它),然后使用它来设置删除调用。

Dropzone.options.myDropzone = {
  init: function() {
    this.on("success", function(file, response) {
      file.serverId = response; // If you just return the ID when storing the file
      // You can also return a JSON object then the line would
      // look something like this:
      //
      // file.serverId = response.id;
      //
      // In that case make sure that you respond with the mime type
      // application/json
    });
    this.on("removedfile", function(file) {
      if (!file.serverId) { return; } // The file hasn't been uploaded
      $.post("delete-file.php?id=" + file.serverId); // Send the file id along
    });
  }

关于php - Dropzone.js 用 php 删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17400470/

相关文章:

php/mysql - 比较 2 个表中的多个列并显示最大差异

jquery - 过滤对象数组以将不同的值放入新数组中

jQuery 拖放问题 : mousemove Event not binding for some elements

image - 如果图像是链接,如何从拖放中获取图像 URL?

javascript - HTML5、JavaScript : Drag and Drop File from External Window (Windows Explorer)

php - 获取表中显示的最后一篇帖子的 ID

php - 如何使 CodeIgniter 文件上传类接受所有扩展?

jquery - i18next 不与 RequireJS 一起使用

javascript - 淡入/淡出阵列循环停止

php - 在 MySQL 问题中连接表 (php)