无法使用JQuery-File-Upload从文件列表中删除文件 [英] Cannot remove files from file list using JQuery-File-Upload

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

问题描述

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



示例:


  • 选择文件1。

  • 使用jquery,我用$(FORM_SELECTOR)重新设置表单).trigger('reset')

  • 文件2被选中。
  • 文件1和文件2都是上传的。

  • 问题。



现在我有两份文件1.这不是我想要的。



显然没有太多的使用ajax表单上传,如果它只能工作一次,所以我假设有一些我失踪。



一种重置文件队列的方法?

检查data.files对象时,我可以看到
格式被重置后文件就在那里。我能做些什么来同步插件与输入或清除data.files。
如果我手动清除data.files数组(通过pop或data.files = [])尝试
秒上传不起作用。

我初始化上传表单如下:

  $('#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(post from post,状态,数据);
var i ='< input id =file-select-inputname =files []multiple />';
$('#file-select-输入')。replaceWith(i);
})
});
}
});


解决方案

我有同样的问题,如果文件已经上传,我们设法解决的是检查提交回调。我只是检查文件名是否包含在数组 fileNames 中,我在提交之前推送当前文件名,并在下一次检查下一个是否存在数组,如果这样取消提交。

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


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.

Example:

  • File 1 is selected.
  • File 1 is uploaded.
  • Success.
  • Using jquery I reset the form with $(FORM_SELECTOR).trigger('reset')
  • File 2 is selected.
  • File 1 and file 2 are BOTH uploaded.
  • Problem.

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

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?

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);
                })
        });
    }
});

解决方案

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从文件列表中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆