【问题标题】:building a photo uploader using the jquery-file-upload plugin and custom ajax使用 jquery-file-upload 插件和自定义 ajax 构建照片上传器
【发布时间】:2013-12-24 08:19:47
【问题描述】:

我正在构建一个照片上传表单,并决定使用 jQuery 插件jquery-file-upload,但遇到了一些问题......和问题。

首先,我正在使用的代码(与basic 版本一起使用):

// File upload function
jQuery(function() {

    jQuery('#pick-photos').click(function(){

        jQuery('#file-upload').click();
    });

    // Initialize the jQuery File Upload plugin
    jQuery('#photo-upload').fileupload({

        dataType: 'json',
        add: function (e, data) {

            jQuery.each(data.result.files, function (index, file) {
            jQuery('<p/>').text(file.name).appendTo(document.body);
        });

            // Append the file name and file size
            tpl.find('p').text(data.files[0].name)
                         .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

            jQuery('#upload').click(function() {

                data.submit();
            });         
        },

        progressall: function(e, data) {

            // Calculated the completion percentage of the upload
            var progress = parseInt(data.loaded / data.total * 100, 10);

            jQuery('#upload-progress .progress-bar').css(

                'width',
                progress + '%'
           );
        },

    });

    // Helper function that formats the file sizes
    function formatFileSize(bytes) {

        if (typeof bytes !== 'number') {

            return '';
        }

        if (bytes >= 1000000000) {

            return (bytes / 1000000000).toFixed(2) + ' GB';
        }

        if (bytes >= 1000000) {

        return (bytes / 1000000).toFixed(2) + ' MB';
        }

        return (bytes / 1000).toFixed(2) + ' KB';
    }

});

我面临的问题是这行代码jQuery('#photo-upload').fileupload({上的错误Uncaught TypeError: Object [object Object] has no method 'fileupload'

其次,我想知道如何在此代码中包含自定义 ajax...以便我可以根据成功或错误更改页面内容。

我们将接受任何帮助!

【问题讨论】:

    标签: php jquery ajax file-upload jquery-file-upload


    【解决方案1】:

    为了在成功或错误(或两者)时执行某些代码, 提供了一些回调:

    • done(相当于成功)
    • 失败(相当于错误)
    • 总是(等同于完成)

    这些回调中的每一个都具有与添加回调相同的参数。

    基础版wiki举个例子:

    $(function () {
        $('#fileupload').fileupload({
            dataType: 'json',
            add: function (e, data) {
                data.context = $('<p/>').text('Uploading...').appendTo(document.body);
                data.submit();
            },
            done: function (e, data) {
                data.context.text('Upload finished.');
            }
        });
    });
    

    对于您的问题,您是否包含这些文件?

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="js/vendor/jquery.ui.widget.js"></script>
    <script src="js/jquery.iframe-transport.js"></script>
    <script src="js/jquery.fileupload.js"></script>
    

    【讨论】:

    • 好的,所以我确保已按我应该的方式连接了列出的所有文件;但我仍然收到错误。我还有一个我没有提到的错误,但这实际上可能是主要问题...@ 987654326@ jquery.fileupload.js 的第 62 行,上面写着$.widget('blueimp.fileupload', { 注意:不,我没有搞砸那个文件。
    • 我正在使用 wordpress 并且正在使用他们已经拥有的 jquery 版本,恰好是 1.10.2 这可能是原因吗?
    • 你的 JQuery 版本应该没问题。我在这里做了一些测试:jsfiddle.net/Fractaliste/MEA58/3 但我必须发明 HTML。我认为您不能将data.response 调用到add 回调中。而且我不明白tpl 对象是什么。
    • 嗯...我刚刚测试了jsfiddle,它几乎可以按照我想要的方式工作...但是,当我复制和粘贴时,它仍然给我同样的错误!我不知道现在该去哪里或做什么。
    • 您确定所有必需的 .js 文件都已正确下载吗? (Chrome 上的 f12/网络选项卡)。如果是这样,您是否使用其他 JS 库?您的网站在线吗?
    猜你喜欢
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2013-08-01
    • 2016-05-13
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多