From 89c9555ee409f9f4d4e6c479f467a7d6cd63b142 Mon Sep 17 00:00:00 2001 From: taevas Date: Mon, 15 Jul 2019 11:41:40 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=94=AF=E6=8C=81=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=20form=20=E8=A1=A8=E5=8D=95=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=97=B6=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- upyun/browser-form-upload.js | 5 +++-- upyun/form-upload.js | 6 +++--- upyun/upyun.js | 4 ++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fe1258f..c20b92a 100644 --- a/README.md +++ b/README.md @@ -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)的回调函数 @@ -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"` + **响应** diff --git a/upyun/browser-form-upload.js b/upyun/browser-form-upload.js index 2db3cb6..9f6d2b8 100644 --- a/upyun/browser-form-upload.js +++ b/upyun/browser-form-upload.js @@ -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) diff --git a/upyun/form-upload.js b/upyun/form-upload.js index 72890f5..9d8bd16 100644 --- a/upyun/form-upload.js +++ b/upyun/form-upload.js @@ -1,7 +1,7 @@ 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) @@ -9,8 +9,8 @@ export default function formUpload (remoteUrl, localFile, {authorization, 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, { diff --git a/upyun/upyun.js b/upyun/upyun.js index e09e172..5e453a4 100644 --- a/upyun/upyun.js +++ b/upyun/upyun.js @@ -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] @@ -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) }) }