简体   繁体   中英

jQuery file upload plugin error

I am trying to implement the basic fileupload from blueimp file upload plugin:

http://blueimp.github.io/jQuery-File-Upload/basic.html

each time I run the code ,I select 3 files to upload I get this error: Unable to get property 'files' of undefined or null reference.

Can anyone help me with this? Thanks

var url = 'FileUploadHandler.ashx?upload=start';
        $('#fileupload').fileupload({       
            dataType: 'json',
            multipart:true,
            limitMultiFileUploads: 3,
            url: url,
            add: function (e, data) {
                $.each(data.result.files, function (index, file) {
                    $('<p/>').text(file.name).appendTo('#files');
                });
                data.submit();
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#progress .progress-bar').css(
                    'width',
                    progress + '%'
                );
            }
        }).prop('disabled', !$.support.fileInput)
            .parent().addClass($.support.fileInput ? undefined : 'disabled');
    });

Try this:

Demo

var url = 'FileUploadHandler.ashx?upload=start';
$('#fileupload').fileupload({       
    dataType: 'json',
    multipart:true,
    limitMultiFileUploads: 3,
    url: url,
    add: function (e, data) {
        $.each(data.files, function (index, file) {
            $('<p/>').text(file.name).appendTo('#files');
        });
        data.submit();
    },
    progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .progress-bar').css(
            'width',
            progress + '%'
        );
    }
}).prop('disabled', !$.support.fileInput).parent().addClass($.support.fileInput ? undefined : 'disabled');
<script>
    /*jslint unparam: true */
    /*global window, $ */
    $(function () {
        'use strict';
        // Change this to the location of your server-side upload handler:


        var url = 'FileUploadHandler.ashx?upload=start',

        uploadButton = $('<button/>')
               .addClass('btn btn-primary')
               .prop('disabled', false)
               .text('Upload')
               .on('click', function () {
                   var $this = $(this),
                       data = $this.data();
                   $this
                        .off('click')
                        .text('Abort')
                        .on('click', function () {
                            $this.remove();
                            data.abort();
                        });
                   data.submit().always(function () {
                          $this.remove();
                   });
               });



        $('#fileupload').fileupload({
            url: url,
            dataType: 'json',
            autoUpload: false,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i
        }).on('fileuploadadd', function (e, data) {
            data.context = $('<div/>').appendTo('#files');
            $.each(data.files, function (index, file) {
                var node = $('<p/>')
                        .append($('<span/>').text(file.name));
                if (!index) {
                    node
                         .append('<br>')
                         .append(uploadButton.clone(true).data(data));

                }
                node.appendTo(data.context);
            });
        }).on('fileuploadprogressall', function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
            );

        });
    });


</script>

</head>
<body>
    <form id="form1" runat="server">
            <span class="btn btn-success fileinput-button"> 
         <i class="glyphicon glyphicon-plus"></i> 
         <span>Select files...</span> 
         <!-- The file input field used as target for the file upload widget --> 

         <asp:FileUpload ID="fileupload" runat="server" AllowMultiple="true" />

     </span> 
     <br/> 
     <br/> 
     <!-- The global progress bar --> 
     <div id="progress" class="progress"> 
         <div class="progress-bar progress-bar-success"></div> 
     </div> 
     <!-- The container for the uploaded files --> 
     <div id="files" class="files"></div> 

        <br/> 

    </form>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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