You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

页面重新加载后如何防止Dropzone超出maxFiles设置的文件数量上限

问题原因

你手动调用事件添加服务端已有文件时,没有同步更新Dropzone内部维护的files统计数组,Dropzone判断已上传文件数量仅依赖这个内部数组,所以它会认为当前上传区没有文件,就不会触发maxFiles的限制逻辑。
另外你代码中还有两处小问题也会影响功能:ajax回调的success拼写错误、删除文件时没有同步更新内部统计数组,会导致删除旧文件后无法上传新文件。

修复步骤
  • 添加mock文件时同步推入Dropzone的内部统计数组
  • 删除文件时同步从内部统计数组中移除对应文件
  • 修正sucess的拼写错误为success
修正后的完整代码
Dropzone.autoDiscover = false;

$("#Titelbild").dropzone({
  maxFiles: 1,
  maxFilesize: 2, // 文件大小限制2MB
  addRemoveLinks: true,
  acceptedFiles: ".jpeg,.jpg,.png,.gif,.jfif",
  
  removedfile: function(file) {
    var fileName = file.name; 
       
    $.ajax({
      type: 'post',
      url: 'Delete_picture.php',
      data: {name: fileName, request: 'delete'},
      success: function(data){
        console.log('success: ' + data);
      }
    });

    // 同步移除内部统计数组中的文件
    this.files.splice(this.files.indexOf(file), 1);

    var _ref;
    return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
  },  

  init: function() { 
    myDropzone1 = this;
  
    $.ajax({
      url: 'Fetch_picture.php',
      type: 'post',
      data: {request: 'fetch'},
      dataType: 'json',      
      success: function(response){
        $.each(response, function(key,value) {
          var mockFile = { name: value.name, size: value.size};
          myDropzone1.emit("addedfile", mockFile);
          myDropzone1.emit("thumbnail", mockFile, value.path);
          myDropzone1.emit("complete", mockFile);
          // 同步推入内部统计数组
          myDropzone1.files.push(mockFile);
        });
      }
    });
  }
});

内容的提问来源于stack exchange,提问作者Gunnar Bock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 18:15:02