Skip to content

Commit 89c9555

Browse files
committed
✨ 支持自定义 form 表单上传时的文件名
1 parent 174d110 commit 89c9555

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ client.getFile('/sample.txt', saveTo).then(function (stream) {
281281
})
282282
```
283283

284-
### formPutFile(remotePath, localFile, params = {})
284+
### formPutFile(remotePath, localFile, params = {}, opts = {})
285285

286286
使用又拍云[表单 api](http://docs.upyun.com/api/form_api/) 上传文件。客户端使用该方法时,
287287
必须先设置获取又拍云 [HTTP Body 签名](http://docs.upyun.com/api/authorization/#http-body)的回调函数
@@ -291,6 +291,9 @@ client.getFile('/sample.txt', saveTo).then(function (stream) {
291291
- `remotePath`: 保存路径
292292
- `localFile`: 需要上传的文件,和 `putFile` 相同(**如果在浏览器端使用,只支持 String/Blob/File **)
293293
- `params`: 又拍云表单 api 支持的可选参数(`service(同 bucket)`, `save-key` 两个必选参数不需要手动在这里设置)
294+
- `opts`
295+
- `filename` 可选参数. 用于指定上传字符串/ Blob 的文件名. 支持路径取 basename. 文件名取值优先级 `filename` > `localFile` 是文件 > 默认值 `"file"`
296+
294297

295298
**响应**
296299

upyun/browser-form-upload.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import axios from 'axios'
22

3-
export default function formUpload (remoteUrl, localFile, {authorization, policy}) {
3+
export default function formUpload (remoteUrl, localFile, {authorization, policy}, {filename} = {}) {
44
const data = new FormData()
55
data.append('authorization', authorization)
66
data.append('policy', policy)
77
if (typeof localFile === 'string') {
88
localFile = new Blob([localFile], {type: 'text/plain'})
99
}
10-
data.append('file', localFile)
10+
11+
data.append('file', localFile, filename)
1112
return axios.post(remoteUrl, data).then(({status, data}) => {
1213
if (status === 200) {
1314
return Promise.resolve(data)

upyun/form-upload.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import FormData from 'form-data'
22
import path from 'path'
33

4-
export default function formUpload (remoteUrl, localFile, {authorization, policy}) {
4+
export default function formUpload (remoteUrl, localFile, {authorization, policy}, {filename} = {}) {
55
return new Promise((resolve, reject) => {
66
const data = new FormData()
77
data.append('authorization', authorization)
88
data.append('policy', policy)
99
// NOTE when type of localFile is buffer/string,
1010
// force set filename=file, FormData will treat it as a file
1111
// real filename will be set by save-key in policy
12-
const filename = (localFile.name || localFile.path) ?
13-
path.basename(localFile.name || localFile.path) :
12+
filename = (filename || localFile.name || localFile.path) ?
13+
path.basename(filename || localFile.name || localFile.path) :
1414
'file'
1515

1616
data.append('file', localFile, {

upyun/upyun.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ export default class Upyun {
436436
})
437437
}
438438

439-
formPutFile (remotePath, localFile, orignParams = {}) {
439+
formPutFile (remotePath, localFile, orignParams = {}, opts = {}) {
440440
const params = {}
441441
for (const key of Object.keys(orignParams)) {
442442
params[key.toLowerCase()] = orignParams[key]
@@ -452,7 +452,7 @@ export default class Upyun {
452452
result = isPromise(result) ? result : Promise.resolve(result)
453453

454454
return result.then((bodySign) => {
455-
return formUpload(this.endpoint + '/' + params['service'], localFile, bodySign)
455+
return formUpload(this.endpoint + '/' + params['service'], localFile, bodySign, opts)
456456
})
457457
}
458458

0 commit comments

Comments
 (0)