ajaxfileupload problemajaxFileUpload不是一个函数

时间:2013-04-04 12:33:14

标签: ajax codeigniter ajax-upload

我使用ajaxfileupload上传文件,每当我上传文件时它都会给我这个错误。我尝试了很多但没有运气。

 TypeError: $.ajaxFileUpload is not a function  
 success: function (data, status)

这是代码我的代码。

            $id =$("#id").val();
    $.ajaxFileUpload
    (
        {
            type: "POST",
            url: "main_page/save_image_data",
            secureuri:false,
            fileElementId:'userfile'+id,
            dataType: 'json',
            data: { "image_id":id},
            success: function (data, status)
            {
                if(typeof(data.error) != 'undefined')
                {
                    if(data.error != '')
                    {
                        alert(data.error);
                    }else
                    {
                        alert(data.msg);
                    }
                }
            },
            /*error: function (data, status, e)
            {
                alert(e);
            }*/
        }
    )

    return false;

4 个答案:

答案 0 :(得分:1)

jQuery的核心中没有命令$.ajaxFileUpload

因此,您可能正在尝试使用外部库,而您尚未将其包含在<head>中。

答案 1 :(得分:0)

将此添加到您的文件中。此方法已从jquery的高级版本中删除。

jQuery.extend({
    handleError: function( s, xhr, status, e ) {
        // If a local callback was specified, fire it
        if ( s.error )
            s.error( xhr, status, e );
        // If we have some XML response text (e.g. from an AJAX call) then log it in the console
        else if(xhr.responseText)
            console.log(xhr.responseText);
    }
   });

答案 2 :(得分:0)

如果您在页面中包含jquery.min.js,则会发生这种情况。删除后,该文件问题将得到解决。

答案 3 :(得分:0)

这是我的解决方案-----遵循官方的格式:

<script type="text/javascript">

    $(document).ready(function() {
        var interval;

        function applyAjaxFileUpload(element) {
            $(element).AjaxFileUpload({
                action: "MyJsp.jsp",
                onChange: function(filename) {
                    // Create a span element to notify the user of an upload in progress
                    var $span = $("<span />")
                        .attr("class", $(this).attr("id"))
                        .text("Uploading")
                        .insertAfter($(this));

                    $(this).remove();

                    interval = window.setInterval(function() {
                        var text = $span.text();
                        if (text.length < 13) {
                            $span.text(text + ".");
                        } else {
                            $span.text("Uploading");
                        }
                    }, 200);
                },
                onSubmit: function(filename) {
                    // Return false here to cancel the upload
                    /*var $fileInput = $("<input />")
                        .attr({
                            type: "file",
                            name: $(this).attr("name"),
                            id: $(this).attr("id")
                        });

                    $("span." + $(this).attr("id")).replaceWith($fileInput);

                    applyAjaxFileUpload($fileInput);

                    return false;*/

                    // Return key-value pair to be sent along with the file
                    return true;
                },
                onComplete: function(filename, response) {
                    window.clearInterval(interval);
                    var $span = $("span." + $(this).attr("id")).text(filename + " "),
                        $fileInput = $("<input />")
                            .attr({
                                type: "file",
                                name: $(this).attr("name"),
                                id: $(this).attr("id")
                            });

                    if (typeof(response.error) === "string") {
                        $span.replaceWith($fileInput);

                        applyAjaxFileUpload($fileInput);

                        alert(response.error);

                        return;
                    }

                    $("<a />")
                        .attr("href", "#")
                        .text("x")
                        .bind("click", function(e) {
                            $span.replaceWith($fileInput);

                            applyAjaxFileUpload($fileInput);
                        })
                        .appendTo($span);
                }
            });
        }

        applyAjaxFileUpload("#demo1");
    });

</script>

至少,没有更多的错误。该操作将收到请求