簡體   English   中英

如何正確將 base64 編碼文件從快遞發送到客戶端並啟動下載?

[英]How to correctly send base64 encoded document from express to the client and initiate download?

我有一個包含 3 個應用程序的系統。

  1. 前端的反應客戶端
  2. Flask 后端用於 API
  3. 生成word文檔的節點應用(docx)

后端 flask 應用程序本質上充當所有 API 請求的代理。 客戶端將向后端的端點發送請求,該端點又向快速應用程序發出請求。

express 應用程序生成一個 Word 文檔,如下所示:

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

我的 Flask 端點邏輯如下所示:

    // 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

我需要能夠從客戶端下載此文檔。 我該怎么做,或者我應該如何解碼 base64 字符串? 到目前為止我所嘗試的一切都失敗了。

將 docx 文件發送到瀏覽器使用toBlob()方法而不是使用toBase64String()方法並再次轉換為緩沖區。 根據這個位置的文檔

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

如果您正在創建 docx 文件,則 mime 類型應為application/vnd.openxmlformats-officedocument.wordprocessingml.document

你可以從這里參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM