0

I have a system with 3 applications.

  1. React client for frontend
  2. Flask backend for API
  3. Node application for generating a word document (docx)

The backend flask application essentially acts as a proxy for all API requests. The client will send a request to an end-point in the backend that in turn will make a request to the express app.

The express application generates a Word doc like so:

  // http://localhost:4000/word-export
  Packer.toBase64String(doc)
    .then(resp => {
      const fileContents = Buffer.from(resp, 'base64');
      res.send(fileContents);
    })
    .catch(error => res.send('error'));

My Flask endpoint logic looks like:

    // http://localhost:5000/api/export
    response = requests.get(
        'http://localhost:4000/word-export', headers=headers, json=json_data)

    response = make_response(response.content)

    response.headers.set('Content-Type', 'application/msword')
    # response.headers.set('Content-Disposition', 'attachment', filename='doc.docx')

    return response

I need to be able to download this document from the client. How can I do this, or how should I decode the base64 string? Everything I have tried so far has failed.

Rob Fyffe
  • 661
  • 1
  • 6
  • 17

1 Answers1

0

for sending docx file to browser use toBlob() method instead of using toBase64String() method and again converting to buffer. As per the doc at this location

packer.toBlob(doc).then((blob) => {
    // saveAs from FileSaver will download the file
      saveAs(blob);
});

and if you are creating a docx file the mime type should be application/vnd.openxmlformats-officedocument.wordprocessingml.document

you can refer from here

Ashutosh Padhi
  • 161
  • 1
  • 7