• axios 实现上传、下载


     一、下载(支持批量下载)

    1. const downloadFile = (fileList) => {
    2. console.log(fileList, '下载list')
    3. fileList.forEach((e) => {
    4. const uid = uuidv4()
    5. const CancelToken = axios.CancelToken
    6. let source = CancelToken.source()
    7. transmissionStore().setDownloadSource('add', {
    8. workId: uid,
    9. axiosSource: source,
    10. })
    11. axios({
    12. url: `/xxxxxxxxx`,
    13. method: 'get',
    14. params: {
    15. id: uid,
    16. filePath: e.filePath,
    17. isDir: e.isDir,
    18. },
    19. responseType: 'blob',
    20. headers: {
    21. 'Content-Type': 'application/json; charset=utf-8',
    22. workId: uid,
    23. },
    24. cancelToken: source.token,
    25. onDownloadProgress: function (progressEvent) {
    26. downloadLodashdebounce(uid, progressEvent) //节流推送
    27. if (progressEvent.total == progressEvent.loaded) {
    28. setTimeout(() => {
    29. transmissionStore().setDownloadSource('delete', {
    30. workId: uid,
    31. })
    32. }, 1000)
    33. }
    34. },
    35. })
    36. .then((res) => {
    37. const blob = new Blob([res.data], {
    38. type: 'application/octet-stream',
    39. })
    40. const url = window.URL.createObjectURL(blob)
    41. const a = document.createElement('a')
    42. a.href = url
    43. a.download = e.isDir ? `${e.fileName}.zip` : e.fileName
    44. document.body.appendChild(a)
    45. a.click()
    46. document.body.removeChild(a)
    47. window.URL.revokeObjectURL(url)
    48. })
    49. .catch((error) => {
    50. console.log(error, '下载接口中断')
    51. })
    52. })
    53. proxy.$message({
    54. message: '已添加至传输列表',
    55. type: 'success',
    56. })
    57. }
    58. // 中断接口下载
    59. source.cancel(`取消下载`)

    二、上传(支持批量上传)

    1. const uploadFile = (e, is_file) => {
    2. const path = info.filePathList.join('/')
    3. const pathValue = path ? `${userData.value.username}/` + path : ''
    4. let file_lists = Object.values(e.target.files)
    5. console.log(file_lists, '上传list')
    6. if (file_lists.length > 0 && file_lists.length <= 30) {
    7. file_lists.forEach((item) => {
    8. const uid = uuidv4()
    9. const formData = new FormData()
    10. formData.append('fileName', item.name)
    11. formData.append('file', item)
    12. formData.append('filePath', pathValue)
    13. if (!is_file) {
    14. let folderName = item.webkitRelativePath.split('/')
    15. folderName.pop()
    16. formData.append('dirName', folderName.join('/'))
    17. }
    18. const CancelToken = axios.CancelToken
    19. let source = CancelToken.source()
    20. transmissionStore().setUploadSource('add', {
    21. workId: uid,
    22. axiosSource: source,
    23. })
    24. axios({
    25. url: `/xxxxx/upload`,
    26. method: 'post',
    27. data: formData,
    28. headers: {
    29. workId: uid,
    30. },
    31. cancelToken: source.token,
    32. onUploadProgress: function (progressEvent) {
    33. lodashdebounce(uid, progressEvent, formData) //节流推送
    34. if (progressEvent.total == progressEvent.loaded) {
    35. setTimeout(() => {
    36. transmissionStore().setUploadSource('delete', {
    37. workId: uid,
    38. })
    39. }, 1000)
    40. }
    41. },
    42. })
    43. .then((res) => {})
    44. .catch((error) => {
    45. console.log(error, '上传接口中断')
    46. transmissionStore().setOperationObj({
    47. type: 'filecancel',
    48. data: {
    49. id: uid,
    50. fileName: formData.get('fileName'),
    51. },
    52. })
    53. })
    54. })
    55. nextTick(() => {
    56. file_ref.value.value = null
    57. folder_ref.value.value = null
    58. })
    59. proxy.$message({
    60. message: '已添加至传输列表',
    61. type: 'success',
    62. })
    63. } else {
    64. proxy.$message({
    65. message: '文件数量超过限制,最多可上传30个。',
    66. type: 'warning',
    67. })
    68. nextTick(() => {
    69. file_ref.value.value = null
    70. folder_ref.value.value = null
    71. })
    72. }
    73. }
    74. // 中断接口上传
    75. source.cancel(`取消上传`)

  • 相关阅读:
    2022/09/01 day01:Git概述
    BIOS 如何确定引导扇区的位置
    解决cscript打开excel时宏互不可见的问题
    Laravel artisan 常用命令
    【Vuforia+Unity】AR04-地面、桌面平面识别功能(Ground Plane Target)
    hbase最新版本配置属性
    笔记一:odoo透视表和图表
    springboot启动流程
    交换机与路由技术-12-单臂路由
    深度学习_11_softmax_图片识别代码&原理解析
  • 原文地址:https://blog.csdn.net/yzding1225/article/details/138082049