Skip to content

Commit

Permalink
feat(request): support upload file in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 2, 2018
1 parent 9b098e9 commit 4513e54
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ export default function request<T extends RequestOptions>(options: T): Promise<{
}

// 解析文件参数
let file: { key: string, value: FileData }
let file: { key: string, value: any }
forOwn(options.data, (value, key) => {
if (value instanceof FileData) {
file = { key, value }
if (value instanceof FileData || (inBrowser() && value instanceof File)) {
file = {
key: key,
value: value instanceof FileData ? value.get() : value,
}
return false
}
})
if (file) {
options.data = omit(options.data, [file.key])
}

// 设置 Content-Type
options.header['Content-Type'] = options.header['Content-Type'] || (
Expand All @@ -66,9 +66,10 @@ export default function request<T extends RequestOptions>(options: T): Promise<{
// 小程序请求
if (inWechatMiniProgram()) {
if (file) {
options.data = omit(options.data, [file.key])
wx.uploadFile({
url: options.url,
filePath: file.value.get() as string,
filePath: file.value,
name: file.key,
header: options.header,
formData: options.data,
Expand Down Expand Up @@ -107,16 +108,23 @@ export default function request<T extends RequestOptions>(options: T): Promise<{

// 浏览器请求
if (inBrowser()) {
let requestBody: string = null
let url = options.url
if (options.requestDataType === 'json') {
requestBody = JSON.stringify(options.data)
let requestBody: string | FormData = null
if (file) {
requestBody = Object.keys(options.data).reduce((fd, key) => {
fd.append(key, file.key === key ? file.value : options.data[key])
return fd
}, new FormData())
} else {
const queryString = objectToQueryString(options.data)
if (options.method === 'GET') {
url += (url.indexOf('?') > -1 ? '&' : '?') + queryString
if (options.requestDataType === 'json') {
requestBody = JSON.stringify(options.data)
} else {
requestBody = queryString
const queryString = objectToQueryString(options.data)
if (options.method === 'GET') {
url += (url.indexOf('?') > -1 ? '&' : '?') + queryString
} else {
requestBody = queryString
}
}
}
const xhr = new XMLHttpRequest()
Expand Down

0 comments on commit 4513e54

Please sign in to comment.