Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for size to be determined by a function #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion docs/docs/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ Allow upload file extensions

Allow the maximum byte to upload

* **Type:** `Number`
* **Type:** `Number | Function`

* **Default:** `0`

Expand All @@ -558,10 +558,36 @@ Allow the maximum byte to upload
* **Details:**

`0` is equal to not limit
If a `Function` is given, it will be called with the `File` object passed as an argument, and it should return a `Number`

* **Usage:**
```html
<file-upload :size="1024 * 1024"></file-upload>
<!--or-->
<template>
<file-upload :size="this.calcSize"></file-upload>
</template>
<script>
export default {
methods: {
calcSize(file) {
let size = 0;
let mimePrefix = file.type.substr(0, file.type.indexOf('/'));

switch (mimePrefix) {
case 'image':
size = 1024 * 1024; // 1 MiB
break;
case 'video':
size = 1024 * 1024 * 500; // 500 MiB
break;
}

return size;
}
}
}
</script>
```


Expand Down
28 changes: 27 additions & 1 deletion docs/docs/zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ input标签的 `name` 属性

允许上传的最大字节

* **类型:** `Number`
* **类型:** `Number | Function`

* **默认值:** `0`

Expand All @@ -476,10 +476,36 @@ input标签的 `name` 属性
* **详细:**

`0` 等于不限制
如果给出了一个`Function`,它就会以作为参数传递的`File`对象被调用,并且应该返回一个`Number`

* **示例:**
```html
<file-upload :size="1024 * 1024"></file-upload>
<!--or-->
<template>
<file-upload :size="this.calcSize"></file-upload>
</template>
<script>
export default {
methods: {
calcSize(file) {
let size = 0;
let mimePrefix = file.type.substr(0, file.type.indexOf('/'));

switch (mimePrefix) {
case 'image':
size = 1024 * 1024; // 1 MiB
break;
case 'video':
size = 1024 * 1024 * 500; // 500 MiB
break;
}

return size;
}
}
}
</script>
```


Expand Down
6 changes: 4 additions & 2 deletions src/FileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default {
},

size: {
type: Number,
type: [Function, Number],
default: 0,
},

Expand Down Expand Up @@ -763,7 +763,9 @@ export default {
}

// 大小
if (this.size > 0 && file.size >= 0 && file.size > this.size) {
let size = (typeof this.size === 'function' ? this.size(file) : this.size)

if (size > 0 && file.size >= 0 && file.size > size) {
return Promise.reject('size')
}

Expand Down