king2088 / vue-3d-loader

VueJS and threeJS 3d viewer plugin
https://king2088.github.io/vue-3d-loader-docs
MIT License
222 stars 38 forks source link

你好请问一下 后端如果返回的是一个文件流的形式给的一个下载文件的链接 如何进行加载呢 #97

Closed 986459879 closed 8 months ago

king2088 commented 8 months ago

你应该需要将二进制转换为文件,然后再使用即可,大概的代码如下,代码未经过实际项目测试,你可以参考一下:

// 直接请求后端地址并转换文件
download (backendUrl) {
    const xmlHttp = new XMLHttpRequest()
    xmlHttp.open('POST', backendUrl, true) // 请自行修改请求方式
    xmlHttp.setRequestHeader('Content-type', 'application/json;charset=UTF-8')
    xmlHttp.responseType = 'blob'
    xmlHttp.onload = oEvent => {
      const arrayBuffer = xmlHttp.response
      if (arrayBuffer) {
        let openUrl = window.URL.createObjectURL(arrayBuffer)
        // **********重要代码,将你的filePath设置为openUrl********************
        this.filePath = openUrl
      }
    }
    xmlHttp.send()
  }

当然,如果你已经通过其他方式获取到了二进制数据,而且也不需要再次进行请求后端地址时,你可以这么使用

// data就是后端返回的二进制内容
const blob = new Blob([data])
const url = window.URL.createObjectURL(blob)
// **********重要代码,将你的filePath设置为openUrl********************
this.filePath = openUrl
986459879 commented 8 months ago

我转换成这样的了 还是报错this.filePath = openUrl openUrl打印出来就是这串 blob:http://localhost:1024/2d0ef784-c89c-44b3-876e-f0e83eec9902

报错这个 Cannot read properties of null (reading 'setCrossOrigin') TypeError: Cannot read properties of null (reading 'setCrossOrigin') at load (webpack-internal:///./node_modules/vue-3d-loader/dist/vue-3d-loader.esm.js:279:80027)

ZhiLunXu commented 8 months ago

有url的时候再渲染就好了