index.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import request from '@repo/api-client'
  2. type UploadResponse = {
  3. id: string
  4. name: string
  5. extensionName: string
  6. size: number
  7. path: string
  8. }
  9. /**
  10. * 上传文件
  11. * @param file
  12. * @returns
  13. */
  14. export const UploadFile = (
  15. data: FormData,
  16. callback?: (p: number) => void
  17. ): Promise<UploadResponse> | undefined => {
  18. // return request<{
  19. // code: number
  20. // result: {
  21. // id: string
  22. // }[]
  23. // }>('/fileApi/File/UploadFiles', {
  24. // method: 'POST',
  25. // headers: {
  26. // 'Content-Type': 'multipart/form-data'
  27. // },
  28. // data
  29. // })
  30. const partSize = 2 * 1024 * 1024
  31. // 进行文件分块传输
  32. // @ts-ignore
  33. if (window?.BpmTools) {
  34. return new Promise((resolve, reject) => {
  35. // @ts-ignore
  36. window?.BpmTools?.$$doFilePartUpload({
  37. file: data.get('files'),
  38. partSize: partSize,
  39. success: function (res: UploadResponse) {
  40. console.log('success', res)
  41. callback?.(100)
  42. resolve(res)
  43. },
  44. progress: function (progress: number) {
  45. console.log('progress', progress)
  46. callback?.(progress)
  47. },
  48. error: function (err: Error) {
  49. console.error(err)
  50. reject(err)
  51. }
  52. })
  53. })
  54. }
  55. }
  56. /**
  57. * 获取图片
  58. * @param fileId 文件id
  59. * @returns
  60. */
  61. export const GetImage = (data: { fileId: string }) => {
  62. return request('/File/GetImage', {
  63. method: 'GET',
  64. params: data,
  65. responseType: 'blob'
  66. })
  67. }
  68. /**
  69. * 下载文件
  70. * @param fileId 文件id
  71. * @returns
  72. */
  73. export const Download = (data: { fileId: string }) => {
  74. return request('/File/Download', {
  75. method: 'GET',
  76. params: data,
  77. responseType: 'blob'
  78. })
  79. }