前端通过本地文件路径获取该文件Buffer

onFileChange = e => {
    ...

    const reader = new FileReader();
    reader.readAsArrayBuffer(e.target.files[0], 'utf-8'); //发起异步请求
    reader.onload = function () {
      //读取完成后,数据保存在对象的result属性中
      const buffer = new Uint8Array(this.result)
      // console.log('buffer:', buffer)
      getEtag(buffer, eTag => { // 获取七牛云fileHash
        console.log('eTag:', eTag)
        const { infoVO: { imId: loginImId = '' } = {} } = storage.get('user') || {}
        const custom = { // 配合移动端扩展字段
          sessionID: scene === 'p2p' ? loginImId : to,
          sessionType: scene === 'p2p' ? 0 : 1
        }
        let options = {
          scene,
          to,
          type,
          fileInput: 'uploadFile',
          custom: JSON.stringify(custom),
          done: () => {
            that.buildSessions()
          }
        }
        if (type === 'file') { // 文件类型添加扩展字段fileHash
          options = {
            ...options,
            custom: JSON.stringify({
              fileHash: eTag,
              ...custom
            }),
          }
        }
        nim.sendFile(options)
      })
    }
  }
posted @ 2019-12-27 16:47  L1204  阅读(2577)  评论(0编辑  收藏  举报