使用Requirejs上传jQuery文件

时间:2014-02-11 15:37:51

标签: jquery jquery-plugins requirejs require jquery-file-upload

我一直在尝试this在Intranet环境中解决这个问题,所以很明显没有CDN。 后来从this文章中我发现我们需要使用RequireJs将'load-image','tmpl'和'canvas-to-blob'加载为AMD。

我有main.js如下:

(function () {
    require.config({
        baseUrl: '/Scripts',
        waitSeconds: 800,
        paths: {
            'jquery': ['/Content/jQueryFileUpload/js/jquery', 'jquery-1.10.2'],
            'jquery.ui.widget': ['/Content/jQueryFileUpload/js/vendor/jquery.ui.widget'],
            'jquery.fileupload': '/Content/jQueryFileUpload/js/jquery.fileupload',
            'jquery.fileupload-ui': '/Content/jQueryFileUpload/js/jquery.fileupload-ui',
            'jquery.fileupload-image': '/Content/jQueryFileUpload/js/jquery.fileupload-image',
            'jquery.fileupload-validate': '/Content/jQueryFileUpload/js/jquery.fileupload-validate',
            'jquery.fileupload-video': '/Content/jQueryFileUpload/js/jquery.fileupload-video',
            'jquery.fileupload-audio': '/Content/jQueryFileUpload/js/jquery.fileupload-audio',
            'jquery.fileupload-process': '/Content/jQueryFileUpload/js/jquery.fileupload-process',
            'jquery.fileupload-jquery-ui': '/Content/jQueryFileUpload/js/jquery.fileupload-jquery-ui',
            'jquery.iframe-transport': ['jquery-1.10.2', '/Content/jQueryFileUpload/js/jquery.iframe-transport'],
            'load-image': '/Content/jQueryFileUpload/js/load-image',
            'load-image-meta': '/Content/jQueryFileUpload/js/load-image-meta',
            'load-image-exif': ['/Content/jQueryFileUpload/js/load-image-meta', '/Content/jQueryFileUpload/js/load-image-exif'],
            'load-image-ios': '/Content/jQueryFileUpload/js/load-image-ios',
            'canvas-to-blob': '/Content/jQueryFileUpload/js/canvas-to-blob',
            'tmpl': '/Content/jQueryFileUpload/js/tmpl',
            'bootstrap': 'Content/bootstrap/js/bootstrap',
            'app': 'app'
        }
    });

    require(['jquery', 'app'], function ($,app) {
        $(function () {
            app.init();
        });
    });

})();

app.js

define(
        ['jquery', 'tmpl', 'load-image', 'canvas-to-blob', 'jquery.iframe-transport', 'jquery.fileupload-ui'], function () {
            return {
                init: function ($) {
                    alert();
                    $('#fileupload').fileupload({
                        url: '/Upload/UploadHandler.ashx',
                        add: function (e, data) {
                            console.log("add", data);
                        },
                        submit: function (e, data) {
                            console.log("submit", data);
                        },
                        change: function (e, data) {
                            console.log("change", data);
                        },
                        send: function (e, data) {
                            console.log("send", data);
                            data.url = url + "'" + data.files[0].name + "'";
                        }

                    });
                }
            }
        }
    );

控制台中没有错误,控制台中没有任何内容记录。未调用app.init(),选择文件时没有任何反应?不确定我遗漏了什么或我的代码有什么问题。我搜索过许多论坛,但无法解决这个问题!

2 个答案:

答案 0 :(得分:1)

According to the documentationsubmit()调用data object时发送请求。

默认add()回调是执行提交操作的回调。您的错误是在不调用提交功能的情况下覆盖它。所以只需添加它:

add: function (e, data) {
    console.log("add", data);
    data.submit();
},

但你可以把它叫到其他地方。

答案 1 :(得分:1)

在我的网站中,我更改了一行

'load-image-exif': ['/Content/jQueryFileUpload/js/load-image-meta', '/Content/jQueryFileUpload/js/load-image-exif'],

通过

'load-image-exif': '/Content/jQueryFileUpload/js/load-image-exif',

并且工作正常。