Skip to content

Commit 8d88f42

Browse files
YunaiVgitee-org
authored andcommitted
!395 重写 el-upload httpRequest 文件上传成功会走成功的钩子,失败走失败的钩子
Merge pull request !395 from puhui999/dev-crm
2 parents 9e86e47 + 1694827 commit 8d88f42

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/api/infra/file/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ export const getFilePresignedUrl = (path: string) => {
3838
export const createFile = (data: any) => {
3939
return request.post({ url: '/infra/file/create', data })
4040
}
41+
42+
// 上传文件
43+
export const updateFile = (data: any) => {
44+
return request.upload({ url: '/infra/file/upload', data })
45+
}

src/components/UploadFile/src/useUpload.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { getAccessToken, getTenantId } from '@/utils/auth'
21
import * as FileApi from '@/api/infra/file'
32
import CryptoJS from 'crypto-js'
43
import { UploadRawFile, UploadRequestOptions } from 'element-plus/es/components/upload/src/upload'
5-
import { ajaxUpload } from 'element-plus/es/components/upload/src/ajax'
64
import axios from 'axios'
75

86
export const useUpload = () => {
@@ -26,11 +24,21 @@ export const useUpload = () => {
2624
return { data: presignedInfo.url }
2725
})
2826
} else {
29-
// 模式二:后端上传(需要增加后端身份认证请求头)
30-
options.headers['Authorization'] = 'Bearer ' + getAccessToken()
31-
options.headers['tenant-id'] = getTenantId()
32-
// 使用 ElUpload 的上传方法
33-
return ajaxUpload(options)
27+
// 模式二:后端上传
28+
// 重写 el-upload httpRequest 文件上传成功会走成功的钩子,失败走失败的钩子
29+
return new Promise((resolve, reject) => {
30+
FileApi.updateFile({ file: options.file })
31+
.then((res) => {
32+
if (res.code === 0) {
33+
resolve(res)
34+
} else {
35+
reject(res)
36+
}
37+
})
38+
.catch((res) => {
39+
reject(res)
40+
})
41+
})
3442
}
3543
}
3644

0 commit comments

Comments
 (0)