How is it right to download the binary docx to js?



  • ответ с сервера

    Hello! Please indicate if anyone has encountered a download of a generated on the docx server in a binary form (see scream). That's how I tried to do it. With the file-saver library and no.

    / Mode 1

        const blob = new Blob([response.data], {
          type: response.headers['content-type']
        })
    
    const a = document.createElement('a')
    const url = window.URL.createObjectURL(blob)
    a.href = url
    a.download = `${type}_${new Date().toISOString()}.docx`
    document.body.appendChild(a)
    a.click()
    
    document.body.removeChild(a)
    window.URL.revokeObjectURL(url)
    

    / Mode 2

        const blob = new Blob([response.data], {
    type: response.headers['content-type']
    })
    const fileName = ${type}_${new Date().toISOString()}.docx

    saveAs(blob, fileName)
    

    In both cases, the file is downloaded, but in word it's empty, and it doesn't open through the google. If this file opens the notebook, there's a binary contents of the first violin.

    But when this request runs into the talend, there's a link that's downloading and opening up properly.

    Budda appreciate the tip.



  • You almost did the right thing.

    async function getFile(url){
      const request = new Request(url);
      const response = await fetch(request);
      const blob = await response.blob();
      const objectURL = URL.createObjectURL(blob);
      const a = document.createElement('a')
      a.setAttribute('href', objectURL);
      a.setAttribute('download',`${type}_${new Date().toISOString()}.docx`);
      document.body.appendChild(a);
      a.click()
      document.body.removeChild(a)
      URL.revokeObjectURL(objectURL);
    }
    

    In fact, for the future, docx is the zip archive containing document.xml and plus in individual files all investments.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2