jQuery FileUploadUI 单个文件上传

发布于 2024-11-07 02:06:39 字数 350 浏览 0 评论 0原文

我正在使用 fileuploadui (https://github.com/blueimp/jQuery-File-Upload )为我的网站上传文件,但我想让它只上传到 1 个文件,而不是多个文件。

我已从输入 html 标记中删除了“多个”(输入类型=“文件”名称=“文件”),但队列仍然接受超过 1 个文件。

我的想法是,每当有人单击“浏览文件”按钮时,它都会用新文件替换当前队列。它将永远是队列中的 1 个文件。

如何使用replaceNode函数?

谢谢。

im using fileuploadui (https://github.com/blueimp/jQuery-File-Upload) to upload files for my website, but i would like to make it to upload to only 1 file, and not multiple.

i have removed "multiple" from the input html tag (input type="file" name="file"), but the queue is still accepting more than 1 file.

what i have in mind is that, whenever someone clicks on the "Browse File" button, it will replace the current queue with the new file. it will be forever 1 file in the queue.

how do i use the replaceNode function ?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

把时间冻结 2024-11-14 02:06:39

试试这个:

$('#file_upload').fileUploadUIX({
    maxNumberOfFiles: 1
});

希望它有效^^

Try this:

$('#file_upload').fileUploadUIX({
    maxNumberOfFiles: 1
});

Hope it works ^^

美煞众生 2024-11-14 02:06:39

感谢@TopDev,

  1. 删除“multiple”属性
  2. 将 id 添加到表中,
  3. 在底部循环内,添加 {% for (var i=0, file; file=o.files[i]; i++) { %}
    {% $('#filelistholder').empty(); %}

Thanks to @TopDev,

  1. Remove the "multiple" attribute <input type="file" name="files[]">
  2. Add id to the table, <tbody id="filelistholder" class="files">
  3. Inside the loop at the bottom, add {% for (var i=0, file; file=o.files[i]; i++) { %}
    {% $('#filelistholder').empty(); %}
骑趴 2024-11-14 02:06:39

我在添加函数中添加了以下内容:

$('#filelistholder').empty();

每次添加新文件时,这都会清除文件列表容器,确保只留下最后一个文件。

希望有帮助。

所以完整的代码如下所示:

<script type="text/javascript">
        $(function () {
            $('#fileupload').fileupload({
                dataType: "json",
                url: "/api/upload",
                limitConcurrentUploads: 1,
                maxNumberOfFiles: 1,
                sequentialUploads: true,
                progressInterval: 100,
                maxChunkSize: 10000,
                add: function (e, data) {
                    ///empty fie container every time a new file is added
                    $('#filelistholder').empty();
                    //add new file
                    $('#filelistholder').removeClass('hide');
                    data.context = $('<div />').text(data.files[0].name).appendTo('#filelistholder');
                    $('</div><div class="progress"><div class="bar" style="width:0%"></div></div>').appendTo(data.context);
                    $('#btnUploadAll').click(function () {
                        data.submit();
                    });

                },
                done: function (e, data) {
                    data.context.text(data.files[0].name + '... Completed');
                    $('</div><div class="progress"><div class="bar" style="width:100%"></div></div>').appendTo(data.context);
                },
                progressall: function (e, data) {
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    $('#overallbar').css('width', progress + '%');
                },
                progress: function (e, data) {
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    data.context.find('.bar').css('width', progress + '%');
                }
            });
        });
    </script>

I added the following in the add function:

$('#filelistholder').empty();

This will clear the file list container every time a new file is added ensuring only the last file is left.

Hope it helps.

So the full code will look as follows:

<script type="text/javascript">
        $(function () {
            $('#fileupload').fileupload({
                dataType: "json",
                url: "/api/upload",
                limitConcurrentUploads: 1,
                maxNumberOfFiles: 1,
                sequentialUploads: true,
                progressInterval: 100,
                maxChunkSize: 10000,
                add: function (e, data) {
                    ///empty fie container every time a new file is added
                    $('#filelistholder').empty();
                    //add new file
                    $('#filelistholder').removeClass('hide');
                    data.context = $('<div />').text(data.files[0].name).appendTo('#filelistholder');
                    $('</div><div class="progress"><div class="bar" style="width:0%"></div></div>').appendTo(data.context);
                    $('#btnUploadAll').click(function () {
                        data.submit();
                    });

                },
                done: function (e, data) {
                    data.context.text(data.files[0].name + '... Completed');
                    $('</div><div class="progress"><div class="bar" style="width:100%"></div></div>').appendTo(data.context);
                },
                progressall: function (e, data) {
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    $('#overallbar').css('width', progress + '%');
                },
                progress: function (e, data) {
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    data.context.find('.bar').css('width', progress + '%');
                }
            });
        });
    </script>
‖放下 2024-11-14 02:06:39

角度和打字稿的答案(轻松适应非打字稿)。这对我有用。

<span class="btn btn-success fileinput-button" type="reset" ng-click="ctrl.formReset(this)">
<i class="glyphicon glyphicon-plus"></i>
<span>Add file...</span>
<input type="file" name="file" ng-disabled="disabled">
</span>

在控制器(打字稿)

formReset(uploadScope):void{
    uploadScope.clear()
    uploadScope.queue = []
}

maxNumberOfFiles: 1 中,删除“多个”并设置 name="file" 也是不应该的。

Answer for angular and typescript (easy adapt for non typescript). This works for me.

<span class="btn btn-success fileinput-button" type="reset" ng-click="ctrl.formReset(this)">
<i class="glyphicon glyphicon-plus"></i>
<span>Add file...</span>
<input type="file" name="file" ng-disabled="disabled">
</span>

In controller (typescript)

formReset(uploadScope):void{
    uploadScope.clear()
    uploadScope.queue = []
}

maxNumberOfFiles: 1, removing "multiple" and setting name="file" also was not anought.

感情旳空白 2024-11-14 02:06:39

将实际输入字段限制为单个文件上传怎么样?

<%= f.file_field :image, :multiple => false %>

What about restricting the actual input field to single file upload?

<%= f.file_field :image, :multiple => false %>
一袭水袖舞倾城 2024-11-14 02:06:39

我就是这样做的:

    //flag for limiting to 1 single file 
    var uploading;
    $('#file1').fileupload({
        dataType: 'json',
        done: function(e, data) {
        //...
        },
        progressall: function(e, data) {
        },
        add: function (e, data) {
            if (!uploading) {
                uploading = 1;
                console.log('add');
                data.submit();
            } else {
                console.log('cancel');
            }
        },
        always: function() {
            uploading = 0;
        },
        sequentialUploads: true
    });

this is how I did it:

    //flag for limiting to 1 single file 
    var uploading;
    $('#file1').fileupload({
        dataType: 'json',
        done: function(e, data) {
        //...
        },
        progressall: function(e, data) {
        },
        add: function (e, data) {
            if (!uploading) {
                uploading = 1;
                console.log('add');
                data.submit();
            } else {
                console.log('cancel');
            }
        },
        always: function() {
            uploading = 0;
        },
        sequentialUploads: true
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文