Programmatic Dojox Uploader - ajax upload not working

北城余情 提交于 2019-12-20 01:38:51

问题


I can't find any docs about creating dojox/form/Uploader programmatically. I tried it by myself, but it looks like that plugin registering mechanism is somehow broken.

require([
    "dojo/dom-construct",
    "dijit/form/Button",
    "dojox/form/Uploader",
    "dojox/form/uploader/FileList",
    "dojox/form/uploader/plugins/IFrame",
    "dojo/domReady!"
    ], function(domConstruct, Button, Uploader, UploaderFileList) {

   var form = domConstruct.create('form', {
        method: 'post',
        enctype: 'multipart/form-data',
        class: 'Uploader'
    }, document.body);     

    var up = new Uploader({
        label: 'Pick files',
        multiple: true,
        url: '/echo/json/'
    }).placeAt(form);

    var list = new UploaderFileList({
        uploader: up
    }).placeAt(form);

    var btn = new Button({
        type: 'submit',
        label: 'upload',
        onClick: function() {
            up.upload();
        }
    }).placeAt(form);


    btn.startup();
    up.startup();
    list.startup();

});​

Example on jsfiddle here.

As far as I understand, source code of dojox/form/Uploader and dojox/form/uploader/plugins/IFrame, the plugin is registered via dojox.form.addUploaderPlugin function, which redeclares the Uploader widget class using self and plugged plugins as its predecessors. But the key method "upload" of Uploader widget never gets overridden by HTML5 plugin (which is included automatically with Iframe plugin).

Is this bug? Or I am doing something wrong?

Thanks for any help!


回答1:


In short; use new dojox.form.Uploader instead of the pulled in variable, otherwise the plugin extending does not apply.

Reason being, you would see programmer doing following in the addUploaderPlugin:

dojox.form.UploaderOrg = dojox.form.Uploader;
var extensions = [dojox.form.UploaderOrg];
dojox.form.addUploaderPlugin = function(plug){

            extensions.push(plug);
            declare("dojox.form.Uploader", extensions, {});
    }

The class that AMD loader returns is and will allways be dojox.form.UploaderOrg and does not know about the extended plugins.

Change to following:

var up = new dojox.form.Uploader({
    label: 'Pick files',
    multiple: true,
    url: '/echo/json/'
}).placeAt(form);

And make sure you have not set djConfig.publishRequireResult = false



来源:https://stackoverflow.com/questions/10343652/programmatic-dojox-uploader-ajax-upload-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!