Skip to content

Commit

Permalink
perf: fs-files-uploader支持自定义getFileName
Browse files Browse the repository at this point in the history
  • Loading branch information
greper committed Sep 23, 2023
1 parent 994f3af commit 1a6a3e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
33 changes: 23 additions & 10 deletions packages/fast-extends/src/uploader/components/fs-file-uploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export default defineComponent({
valueType: {
type: String, // url ,key, object
default: "url"
}
},
/**
* 根据value获取文件名,用于显示在fileList里面
*/
getFileName: {}
},
emits: ["change", "update:modelValue", "success", "exceed"],
setup(props: any, ctx: any) {
Expand All @@ -121,14 +125,23 @@ export default defineComponent({
const currentValue: Ref = ref();
const fileUploaderRef: Ref = ref();
function pickFileName(url: string) {
const suffix = url.substring(url.lastIndexOf("/") + 1);
const wenIndex = suffix.indexOf("?");
if (wenIndex >= 0) {
return suffix.substring(0, wenIndex);
}
return suffix;
}
const pickFileName = computed(() => {
return (
props.getFileName ||
((url: string) => {
if (typeof url !== "string") {
console.warn("获取文件名失败,请配置getFileName");
return url;
}
const suffix = url.substring(url.lastIndexOf("/") + 1);
const wenIndex = suffix.indexOf("?");
if (wenIndex >= 0) {
return suffix.substring(0, wenIndex);
}
return suffix;
})
);
});
function getValueByValueType(item: any) {
if (props.valueType === "object") {
Expand Down Expand Up @@ -176,7 +189,7 @@ export default defineComponent({
for (const item of arr) {
if (!item.name) {
const url = item.url || item.value;
item.name = pickFileName(url);
item.name = pickFileName.value(url);
}
}
return arr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export default defineComponent({
return (
props.getFileName ||
function (value: any) {
if (typeof url !== "string") {
console.warn("获取文件名失败,请配置getFileName");
return url;
}
if (url?.lastIndexOf("/") >= 0) {
return url.substring(url.lastIndexOf("/") + 1);
}
Expand Down

0 comments on commit 1a6a3e4

Please sign in to comment.