前端通過本地文件路徑獲取該文件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)
      })
    }
  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM