无法使用 JQuery-File-Upload 从文件列表中删除文件

2023-01-27前端开发问题
20

本文介绍了无法使用 JQuery-File-Upload 从文件列表中删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在使用 JQuery-File-Upload 插件时遇到问题.我直接使用插件,而不是通过作者提供的 html 示例页面.基本上我有一个带有一些输入的表格其中之一是文件输入.第一次上传工作正常,但是当我尝试第二次上传时,两个文件都被发送(第一个是第二次),而它应该只是第二个.

I have an issue using the JQuery-File-Upload plugin. I am using the plugin directly and not through the author's provided html example pages. Basically I have a form with some inputs one of which is a file input. The first upload works fine but when I attempt a second upload both files are sent (the first one for the second time) when it should only be the second one.

例子:

  • 已选择文件 1.
  • 文件 1 已上传.
  • 成功.
  • 使用 jquery 我使用 $(FORM_SELECTOR).trigger('reset') 重置表单
  • 已选择文件 2.
  • 文件 1 和文件 2 均已上传.
  • 问题.

现在我有两个文件 1 的副本.这不是我想要的.

Now I have two copies of file 1. This is not what I want.

显然,如果仅能工作一次,那么使用 ajax 表单上传并没有多大意义,所以我认为我缺少一些东西.

Obviously there isn't much point of using an ajax form upload if it only works once so I assume that there is something I am missing.

有没有办法重置文件队列?

Is there a way to reset the file queue?

检查 data.files 对象时,我可以看到文件在表单之后被重置.我该怎么做才能将插件与输入同步或清除 data.files.如果我手动清除 data.files 数组(通过 pop 或 data.files = [])尝试第二次上传不起作用.

When examining the data.files object I can see that the files are there after the form is reset. What can I do to sync the plugin with the input or clear out the data.files. If I manually clear out the data.files array (via pop or data.files = []) attempting a second upload does not work.

我这样初始化上传表单:

I init the upload form like this:

    $('#file-upload-form').fileupload({
    url: 'uploads/upload',
    type: 'POST',
    dataType: 'json',
    multipart: true,
    dropZone: null,
    formAcceptCharset: 'utf-8',
    autoUpload: true,
    add: function (e, data) {
        fileUploadData = data;
        $("#upload-file-btn").click(function () {
            data.submit()
                .success(function (e, status, data) {
                    console.log("success response from post", status, data);
                    var i = '<input id="file-select-input" name="files[]" multiple/>';
                    $('#file-select-input').replaceWith(i);
                })
        });
    }
});

推荐答案

我遇到了同样的问题,我设法解决的唯一方法是在文件已上传的情况下检查提交回调.我只是检查文件名是否包含在数组 fileNames 中,我在提交之前推送当前文件名,并在下一次检查下一个文件是否存在于数组中,如果是则取消提交.

I had the same problem and the only way I've managed to solve that was to check in the submit callback if the file was already uploaded. I just check if the file name is included in the array fileNames which I push the current file name before the submission and checks on the next time if the next one is present on the array and if so cancel the submission.

var fileNames = new Array();

$('#uploadfile').fileupload({
  submit: function(e, data) ->
    var fileName = data.files[0].name;
    if ($.inArray(fileName, fileNames) === -1)
      fileNames.push(fileName);
    else
      return false;
});

这篇关于无法使用 JQuery-File-Upload 从文件列表中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125