Margareto
Margareto

Reputation: 37

How do I restrict the file type with uploadcare in javascript?

I am trying to restrict the file type (only mp3 files) when I open a dialog with uploadcare.

        uploadcare.loadFileGroup(soundGroupID)
        .done(function (fileGroup) {
            uploadcare.openDialog(fileGroup.files(), {
                multiple: true,
                multipleMin: 0,
                fileTypes="mp3"
            }).done(
                function (file) {
                    file.promise().done(
                        function (fileInfo) {
                            //TODO Something
                        });
                });
        })
        .fail(function () {
            // Something went wrong.
        });

Is there any way to push a validator? Is there any way to access the widget? As you can see, I am opening a dialog so I can not access the widget anywhere.

Upvotes: 2

Views: 501

Answers (1)

homm
homm

Reputation: 2252

Unfortunately there is no validators for dialogs in current API, only for widgets. But you can create fake widget and use .openDialog() method on it.

uploadcare.loadFileGroup(soundGroupID)
.done(function (fileGroup) {
    var widget = uploadcare.MultipleWidget('<input data-multiple multiple-min="1">');
    widget.validators.push(function(info) {
        if (info.name !== null) {
            if ( ! /\.mp3$/i.test(info.name)) {
                throw Error('mp3-only');
            }
        }
    });
    widget.openDialog(null).done(
        function (file) {
            file.promise().done(
                function (fileInfo) {
                    //TODO Something
                });
        });
})
.fail(function () {
    // Something went wrong.
});

Alternatively, you can pass list of validators in private __validators option, but this is internal API and can be changed in future versions.

Upvotes: 2

Related Questions