OssService.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import http from '@/utils/request'
  2. import {
  3. UrlPath
  4. } from "@/utils/commonuni"
  5. export const download = (id) => {
  6. console.log('download-:' + id)
  7. doDownload(id, `${process.env.VUE_APP_OSS_URL}/download`).then(response => {
  8. // 提取文件名
  9. let filename = response.headers['content-disposition'].match(
  10. /filename=(.*)/
  11. )[1]
  12. filename = decodeURI(filename)
  13. filename = filename.replaceAll('"', '')
  14. // 将二进制流转为blob
  15. const blob = new Blob([response.data])
  16. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  17. // 兼容IE,window.navigator.msSaveBlob:以本地方式保存文件
  18. window.navigator.msSaveBlob(blob, filename)
  19. } else {
  20. // 创建新的URL并指向File对象或者Blob对象的地址
  21. const blobURL = window.URL.createObjectURL(blob)
  22. // 创建a标签,用于跳转至下载链接
  23. const tempLink = document.createElement('a')
  24. tempLink.style.display = 'none'
  25. tempLink.href = blobURL
  26. tempLink.setAttribute('download', filename)
  27. // 兼容:某些浏览器不支持HTML5的download属性
  28. if (typeof tempLink.download === 'undefined') {
  29. tempLink.setAttribute('target', '_blank')
  30. }
  31. // 挂载a标签
  32. document.body.appendChild(tempLink)
  33. tempLink.click()
  34. document.body.removeChild(tempLink)
  35. // 释放blob URL地址
  36. window.URL.revokeObjectURL(blobURL)
  37. }
  38. })
  39. }
  40. const doDownload = (id, url) => {
  41. return http.get(url, {
  42. id: id,
  43. isFile: true
  44. })
  45. }
  46. export const uploadImageModelValue = (id) => {
  47. var ids = null
  48. id = JSON.parse(JSON.stringify(id))
  49. if (typeof(id) == 'string') {
  50. ids = id.split(",")
  51. } else if (id instanceof Array) {
  52. ids = JSON.parse(JSON.stringify(id))
  53. } else {
  54. return []
  55. }
  56. var urlList = []
  57. for (var i = 0; i < ids.length; i++) {
  58. var urlObj = {}
  59. urlObj.url = UrlPath.getBaseUrl() + "oss/preview?id=" + ids[i]+"&code=" + uni.getStorageSync("token")
  60. urlList.push(urlObj)
  61. }
  62. return urlList;
  63. }