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

Cannot remove files from file list using JQuery-File-Upload(无法使用 JQuery-File-Upload 从文件列表中删除文件)
本文介绍了无法使用 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 从文件列表中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)