Skip to content

Commit

Permalink
✨ 支持自定义 form 表单上传时的文件名
Browse files Browse the repository at this point in the history
  • Loading branch information
di4urp committed Jul 15, 2019
1 parent 174d110 commit 89c9555
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ client.getFile('/sample.txt', saveTo).then(function (stream) {
})
```

### formPutFile(remotePath, localFile, params = {})
### formPutFile(remotePath, localFile, params = {}, opts = {})

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


**响应**

Expand Down
5 changes: 3 additions & 2 deletions upyun/browser-form-upload.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import axios from 'axios'

export default function formUpload (remoteUrl, localFile, {authorization, policy}) {
export default function formUpload (remoteUrl, localFile, {authorization, policy}, {filename} = {}) {
const data = new FormData()
data.append('authorization', authorization)
data.append('policy', policy)
if (typeof localFile === 'string') {
localFile = new Blob([localFile], {type: 'text/plain'})
}
data.append('file', localFile)

data.append('file', localFile, filename)
return axios.post(remoteUrl, data).then(({status, data}) => {
if (status === 200) {
return Promise.resolve(data)
Expand Down
6 changes: 3 additions & 3 deletions upyun/form-upload.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import FormData from 'form-data'
import path from 'path'

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

data.append('file', localFile, {
Expand Down
4 changes: 2 additions & 2 deletions upyun/upyun.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export default class Upyun {
})
}

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

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

Expand Down

0 comments on commit 89c9555

Please sign in to comment.