Browse Source

feat(upload): error 回调函数增加返回值参数 (#1988)

* feat(upload): error回调函数增加返回值参数

* docs(upload): error返回值文档

---------

Co-authored-by: huangzepeng <huangzepeng@excellence.com.cn>
pull/2008/head
Pencil-hzp 5 months ago committed by GitHub
parent
commit
1f9bdffc59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 9
      docs/upload/detail/options.md
  2. 57
      src/modules/upload.js

9
docs/upload/detail/options.md

@ -512,12 +512,17 @@ allDone: function(obj){
<td>error</td>
<td colspan="3">
执行上传请求出现异常的回调(一般为网络异常、URL 404等)。返回两个参数如下:
执行上传请求出现异常的回调(一般为网络异常、URL 404等)。返回三个参数如下:
- `index`: 当前文件的索引
- `upload`: 重新上传的方法
- `res`: 返回值(纯文本)<sup>2.9.12+</sup>
```
error: function(index, upload){
error: function(index, upload, res){
console.log(index); // 当前文件的索引
// upload(); 重新上传的方法
console.log(res); // 返回值(纯文本)
console.log(JSON.parse(res)); // 返回值(json)
}
```

57
src/modules/upload.js

@ -323,7 +323,7 @@ layui.define(['lay', 'layer'], function(exports){
'Upload failed, please try again.',
'status: '+ (e.status || '') +' - '+ (e.statusText || 'error')
].join('<br>'));
error(sets.index);
error(sets.index, e.responseText);
allDone(sets.index);
resetFileState(sets.file);
}
@ -386,22 +386,42 @@ layui.define(['lay', 'layer'], function(exports){
}
}, 30);
};
// 统一回调
var done = function(index, res){
that.elemFile.next('.'+ ELEM_CHOOSE).remove();
elemFile.value = '';
// 强制返回的数据格式
var forceConvert = function(src) {
if(options.force === 'json'){
if(typeof res !== 'object'){
if(typeof src !== 'object'){
try {
res = JSON.parse(res);
return {
status: "CONVERTED",
data: JSON.parse(src)
};
} catch(e){
res = {};
return that.msg(text['data-format-error']);
that.msg(text['data-format-error']);
return {
status: "FORMAT_ERROR",
data: {}
};
}
}
}
return { status: "DO_NOTHING", data: {} }
}
// 统一回调
var done = function(index, res){
that.elemFile.next('.'+ ELEM_CHOOSE).remove();
elemFile.value = '';
var convert = forceConvert(res);
switch(convert.status) {
case "CONVERTED":
res = convert.data;
break;
case "FORMAT_ERROR":
return;
}
typeof options.done === 'function' && options.done(res, index || 0, function(files){
that.upload(files);
@ -409,13 +429,24 @@ layui.define(['lay', 'layer'], function(exports){
};
// 统一网络异常回调
var error = function(index){
var error = function(index, res){
if(options.auto){
elemFile.value = '';
}
var convert = forceConvert(res);
switch(convert.status) {
case "CONVERTED":
res = convert.data;
break;
case "FORMAT_ERROR":
return;
}
typeof options.error === 'function' && options.error(index || 0, function(files){
that.upload(files);
});
}, res);
};
var check;

Loading…
Cancel
Save