简体   繁体   English

在Blueimp jQuery File Upload上禁用自动上传

[英]Disabling auto-uploading on Blueimp jQuery File Upload

I am using blueint jquery.fileupload plugin to upload files using asp.net 我正在使用blueint jquery.fileupload插件来使用asp.net上传文件

I have a situation where I have a page to allow the user to select a category (dropdownlistbox), a title (for the uploaded file - TextBox) and a file input (handled by the plugin). 我有一种情况,我有一个页面,允许用户选择一个类别(dropdownlistbox),一个标题(用于上传的文件 - TextBox)和一个文件输入(由插件处理)。

the plugin: https://github.com/ieb/jQueryFileUpload.Net 插件: https//github.com/ieb/jQueryFileUpload.Net

javascript/jquery: 使用Javascript / jQuery的:

 $('#fileup').fileupload({
                    replaceFileInput: false,
                    formData: function (form) {
                        return [{ name: 'dcat', value: $('#ddlCats').val() }, { name: 'title', value: $('#txtTitle').val()}];
                    },
                    dataType: 'json',
                    url: '/_handlers/FileHandler.ashx',
                    add: function (e, data) {
                        var valid = true;
                        var re = /^.+\.((doc)|(xls)|(xlsx)|(docx)|(pdf)|(pts))$/i;
                        $.each(data.files, function (index, file) {
                            if (!re.test(file.name)) {
                                $('#uploaded').html('This file type is not supported');
                                valid = false;
                            }
                        });
                        if (valid)
                            data.submit();
                    },
                    done: function (e, data) {
                        $.each(data.result, function (index, file) {
                            $('#uploaded').html(file);
                        });
                        GetFiles($('#ddlCats').val())
                    },
                    error: function () {
                        alert('An error occured while uploading the document.');
                    }
                });

html: HTML:

<div id="fUpload">
<span style="font-weight:bold;">Yeni Belge:</span><br />
    <table class="ktoeos">
        <tr>
            <td>Category:</td>
            <td> <select id="ddlCats"></select></td>
        </tr>
        <tr>
            <td>Document Description:</td>
            <td><input type="text" id="txtTitle" /></td>
        </tr>
        <tr>
            <td>Select Document:</td>
            <td><input type="file" name="file" id="fileup" /></td>
        </tr>
        <tr>
           <td colspan="2"><input type="submit" id="btnSubmit" value="Upload" /></td>
        </tr>
    </table>
    <div id="uploaded"></div>

My problem is, the file gets uploaded (through an http handler) straight after I select a file. 我的问题是,文件在我选择文件后直接上传(通过http处理程序)。 I can handle it to submit other form data along with it however, I want to fire this event on the button submit as I need to carry out some validation. 我可以处理它以提交其他表单数据但是,我想在按钮提交时触发此事件,因为我需要执行一些验证。 Also the user might want to select a file first and then fill the other parts of the form, which in this case he/she cannot do, due to the form being submitted before he/she can do that. 用户可能还想首先选择一个文件然后填写表单的其他部分,在这种情况下他/她不能这样做,因为在他/她可以这样做之前提交表单。

Since I am not a very good javascript programmer, I have no idea if this functionality is already available (which probably is) in the .js files available with the plugin. 由于我不是一个非常好的javascript程序员,我不知道这个功能是否已经可用(可能是)插件可用的.js文件中。 Any ideas what I need to change or do? 我需要改变或做什么?

The add callback is invoked as soon as files are added to the widget, so you need to override it to pause the upload and then start it with a button click. 一旦将文件添加到窗口小部件,就会调用add回调,因此您需要覆盖它以暂停上载,然后单击按钮启动它。

add: function (e, data) {
    $("#btnSubmit").click(function () {
        // validate file...
        if(valid)
            data.submit();
    });
}

Update: The documentation has a better example of this now: 更新:文档现在有一个更好的例子

This is very easy to do as there is an option " autoUpload: true" which is set as default in the jquery.fileupload-ui.js 这很容易做到,因为有一个选项" autoUpload: true" ,它在jquery.fileupload-ui.js设置为默认值

If you change it to false and you can manually code on the button Click event. 如果将其更改为false ,则可以在按钮Click事件上手动编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM