上传 Uploader

示例

基本形式

<uploader url="/upload"
    on-success={this._onSuccess($event)}
    on-error={this._onError($event)}>
    <button class="u-btn">上传</button>
</uploader>
var component = new RGUI.Component({
    template: template,
    _onSuccess: function($event) {
        RGUI.Notify.success($event.data);
    },
    _onError: function($event) {
        RGUI.Notify.error($event.message);
    }
});

注意:在IE中实现上传功能时,需要将响应头的Content-Type设置为text/plaintext/html,而不能是application/json,否则IE会提示用户下载返回的数据。

禁用组件

<uploader url="/upload" disabled>
    <button class="u-btn" disabled>上传</button>
</uploader>

文件类型限制

<uploader url="/upload" extensions="jpg,gif,png"
    on-success={this._onSuccess($event)}
    on-error={this._onError($event)}>
    <button class="u-btn">上传</button>
</uploader>
var component = new RGUI.Component({
    template: template,
    _onSuccess: function($event) {
        RGUI.Notify.success($event.data);
    },
    _onError: function($event) {
        RGUI.Notify.error($event.message);
    }
});

文件大小限制

<uploader url="/upload" maxSize="10kB"
    on-success={this._onSuccess($event)}
    on-error={this._onError($event)}>
    <button class="u-btn">上传</button>
</uploader>
var component = new RGUI.Component({
    template: template,
    _onSuccess: function($event) {
        RGUI.Notify.success($event.data);
    },
    _onError: function($event) {
        RGUI.Notify.error($event.message);
    }
});

显示上传进度条

<uploader url="/upload"
    on-success={this._onSuccess($event)}
    on-error={this._onError($event)}
    on-progress={this._onProgress($event)}>
    <button class="u-btn">上传</button>
</uploader>
var component = new RGUI.Component({
    template: template,
    _onSuccess: function($event) {
        RGUI.Notify.success($event.data);
    },
    _onError: function($event) {
        RGUI.Notify.error($event.message);
    },
    _onProgress($event) {
        RGUI.Notify.error($event.data);
    },
});

API

Class

Uploader

继承自

Options

new Uploader()

参数类型默认值数据流向描述
dataObject绑定属性
data.urlstring''outside => inside上传路径
data.dataTypestring'json'outside => inside接收数据类型。可以是:`text`、`xml`、`json`。
data.dataObjectoutside => inside附加数据
data.namestring'file'outside => inside上传文件的name
data.extensionsstring
string[]
''outside => inside可上传的扩展名。默认为空,表示可上传任意文件类型的文件;可以为字符串,多个扩展名用`,`隔开,如:'png,jpg,gif,noext';也可以为数组,如:['png', 'jpg', 'gif', 'noext']。'noext'表示允许文件没有扩展名。
data.maxSizestring
number
''outside => inside可上传的最大文件大小。默认为空,表示可上传任意大小的文件;如果为数字,则表示单位为字节;如果为字符串,可以添加以下单位:`kB`、`MB`、`GB`。
data.sendingbooleanfalseoutside <= inside是否正在上传
data.disabledbooleanfalseoutside => inside是否禁用
data.visiblebooleantrueoutside => inside是否显示
data.classstring''outside => inside补充class

Methods

uploader.upload()

弹出文件对话框并且上传文件

参数类型默认值描述
无参数
返回值类型描述
无返回值

Events

uploader.$on('error')

上传错误时触发

属性类型描述
senderobject事件发送对象
nameobjectExtensionError
messageobject错误信息
extensionsobject可上传的扩展名

uploader.$on('error')

上传错误时触发

属性类型描述
senderobject事件发送对象
nameobjectSizeError
messageobject错误信息
maxSizeobject可上传的最大文件大小
sizeobject当前文件大小

uploader.$on('sending')

发送前触发

属性类型描述
senderobject事件发送对象
dataobject待发送的数据

uploader.$on('progress')

发送中触发

属性类型描述
senderobject事件发送对象
dataobject待发送的数据

uploader.$on('error')

上传错误时触发

属性类型描述
senderobject事件发送对象
nameobjectResponseError
messageobject错误信息

uploader.$on('complete')

上传完成时触发

属性类型描述
senderobject事件发送对象
xmlobject返回的xml

uploader.$on('success')

上传成功时触发

属性类型描述
senderobject事件发送对象
dataobject返回的数据