简体   繁体   中英

Programmatic Dojox Uploader - ajax upload not working

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM