【问题标题】:jQuery error: TypeError: a is undefined in jquery-file-uploadjQuery 错误:TypeError:a 在 jquery-file-upload 中未定义
【发布时间】:2015-08-17 17:14:58
【问题描述】:

我已将 jQuery-File-Upload 添加到我正在创建的就业应用程序中。我有文件上传和保存。但是,在处理返回的 JSON 时出现错误。该按钮不清楚“中止”,脚本的其余部分失败。

我在 FireBug 中报告了这个错误:

TypeError: a is undefined
...rCase()},each:function(a,b,c){var d,e=0,f=a.length,g=r(a);if(c){if(g){for(;f>e;e...

此 JSON 返回后:

[{"name":"New Upload Complete:   a9chafa8d62591feca73c5227473d3d4307contact-mri.png","size":"16.88kB","type":"image\/png","delete_url":"\/ch\/upload","delete_type":"DELETE","url":"\/"}]

我欢迎任何建议或指导!

编辑: 这是我用于 jQuery-file-upload 的代码:

<script src="/js/vendor/jquery.ui.widget.js"></script>
<!-- The Load Image plugin is included for the preview images and image resizing functionality -->
<script src="//blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js"></script>
<!-- The Canvas to Blob plugin is included for image resizing functionality -->
<script src="//blueimp.github.io/JavaScript-Canvas-to-Blob/js/canvas-to-blob.min.js"></script>
<!-- The Iframe Transport is required for browsers without support for XHR file uploads -->
<script src="/js/jquery.iframe-transport.js"></script>
<!-- The basic File Upload plugin -->
<script src="/js/jquery.fileupload.js"></script>
<!-- The File Upload processing plugin -->
<script src="/js/jquery.fileupload-process.js"></script>
<!-- The File Upload image preview & resize plugin -->
<script src="/js/jquery.fileupload-image.js"></script>
<!-- The File Upload audio preview plugin -->
<script src="/js/jquery.fileupload-audio.js"></script>
<!-- The File Upload video preview plugin -->
<script src="/js/jquery.fileupload-video.js"></script>
<!-- The File Upload validation plugin -->
<script src="/js/jquery.fileupload-validate.js"></script>

<script>
/*jslint unparam: true, regexp: true */
/*global window, $ */
$(function () {
    'use strict';
    // Change this to the location of your server-side upload handler:
    var uploadurl = "/upload/index/token/<?php echo $this->token; ?>" ,


            uploadButton = $('<button/>')
            .addClass('button button-primary')
            .prop('disabled', true)
            .text('Processing...')
            .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: uploadurl,
        dataType: 'json',
        autoUpload: false,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 999000,
        // Enable image resizing, except for Android and Opera,
        // which actually support image resizing, but fail to
        // send Blob objects via XHR requests:
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 100,
        previewMaxHeight: 100,
        previewCrop: true
    }).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('fileuploadprocessalways', function (e, data) {
        var index = data.index,
            file = data.files[index],
            node = $(data.context.children()[index]);
        if (file.preview) {
            node
                .prepend('<br>')
                .prepend(file.preview);
        }
        if (file.error) {
            node
                .append('<br>')
                .append($('<span class="text-danger"/>').text(file.error));
        }
        if (index + 1 === data.files.length) {
            data.context.find('button')
                .text('Upload')
                .prop('disabled', !!data.files.error);
        }
    }).on('fileuploadprogressall', function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .progress-bar').css(
            'width',
            progress + '%'
        );
    }).on('fileuploaddone', function (e, data) {
        $.each(data.result.files, function (index, file) {
            if (file.url) {
                var link = $('<a>')
                    .attr('target', '_blank')
                    .prop('href', file.url);
                $(data.context.children()[index])
                    .wrap(link);
            } else if (file.error) {
                var error = $('<span class="text-danger"/>').text(file.error);
                $(data.context.children()[index])
                    .append('<br>')
                    .append(error);
            }
        });
    }).on('fileuploadfail', function (e, data) {
        $.each(data.files, function (index) {
            var error = $('<span class="text-danger"/>').text('File upload failed.');
            $(data.context.children()[index])
                .append('<br>')
                .append(error);
        });
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>

【问题讨论】:

  • 我认为,each 应该是.each
  • 如果您想要一个答案,请尝试更具体。将代码部分添加到您的问题中。可能$.each(&lt;HERE&gt;, function()...有错误
  • 问题是 ,each 在 Google 的 jQuery 文件中:
  • 错误:jquery.min.js(第 2 行,col 2814)
  • 我已经编辑了上面的评论并添加了回复以包含更多信息,是什么导致主 jQuery 文件中的错误?

标签: jquery file jquery-file-upload blueimp


【解决方案1】:

想通了:https://github.com/blueimp/jQuery-File-Upload/wiki/Zend-framework 的上传脚本示例输出 JSON,但没有必要的“文件”包装器。

这个 JSON 输出:

 [{"name":"New Upload Complete -quote.png","size":"1.26kB","url":"\/applicant\/a9chafa8d62591feca73c5227473d3d43077\/quote.png","type":"image\/png","delete_url":"\/applicant\/a9chafa8d62591feca73c5227473d3d43077\/quote.png","delete_type":"DELETE"}]

需要:

{"files":[{"url":"/1343777734/size.png","thumbnailUrl":"/1343777734/size.png.80x80.png","name":"tumor-size.png","type":"image/png","size":12714,"deleteUrl":"/1343777734/size.png","deleteType":"DELETE"}]}

我更改了这个 PHP:

<?php
      header('Pragma: no-cache');
      header('Cache-Control: private, no-cache');
      header('Content-Disposition: inline; filename="files.json"');
      header('X-Content-Type-Options: nosniff');
      header('Vary: Accept');
      echo json_encode($datas);
?>

改为:

<?php 
      header('Pragma: no-cache');
      header('Cache-Control: private, no-cache');
      header('Content-Disposition: inline; filename="files.json"');
      header('X-Content-Type-Options: nosniff');
      header('Vary: Accept');
      echo "{\"files\":". json_encode($datas). "}";
?>          

【讨论】:

    猜你喜欢
    • 2014-12-17
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2011-12-27
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多