在不将 Word 文档保存到 Flask 服务器上的情况下发送。

huangapple go评论84阅读模式
英文:

Sending a word document without saving it on the flask server

问题

Good day. Today I'm trying to send a document generated on the server to the user on the click of a button using Flask.

My task is this:
Create a document (without saving it on the server). And send it to the user.
However, using JavaScript, I track the button click on the form and use fetch to make a request to the server. The server retrieves the necessary data and creates a Word document based on it. How can I form a response to a request so that the file starts downloading?

Code since the creation of the document. (The text of the Word document has been replaced)
Python Flask:

  1. document = Document()
  2. document.add_heading("Some head-title")
  3. document.add_paragraph('Some text')
  4. f = BytesIO()
  5. document.save(f)
  6. f.seek(0)
  7. return send_file(f, as_attachment=True, download_name='some.docx')

However, the file does not start downloading.

How can I send a file from the server to the user?

Edits

This is my JavaScript request.

  1. fetch('/getData', {
  2. method: 'POST',
  3. headers: {
  4. 'Accept': 'application/json',
  5. 'Content-Type': 'application/json'
  6. },
  7. body: JSON.stringify({
  8. someData: someData,
  9. })
  10. })
  11. .then(response =>
  12. response.text()
  13. )
  14. .then(response => {
  15. console.log(response);
  16. });

This is my HTML:

  1. <form action="" name="getData" method="post" enctype="multipart/form-data">
  2. <button type="submit" name="Download">Download</button>
  3. </form>
英文:

Good day. Today I'm trying to send a document generated on the server to the user on the click of a button using Flask.

My task is this:
Create a document (without saving it on the server). And send it to the user.
However, using a java script, I track the button click on the form and use fetch to make a request to the server. The server retrieves the necessary data and creates a Word document based on it. How can I form a response to a request so that the file starts downloading?

Code since the creation of the document. (The text of the Word document has been replaced)
python Falsk:

  1. document = Document()
  2. document.add_heading(&quot;Some head-title&quot;)
  3. document.add_paragraph(&#39;Some text&#39;)
  4. f = BytesIO()
  5. document.save(f)
  6. f.seek(0)
  7. return send_file(f, as_attachment=True, download_name=&#39;some.docx&#39;)

However, the file does not start downloading.

How can I send a file from the server to the user?

Edits

This is my js request.

  1. fetch(&#39;/getData&#39;, {
  2. method : &#39;POST&#39;,
  3. headers: {
  4. &#39;Accept&#39;: &#39;application/json&#39;,
  5. &#39;Content-Type&#39;: &#39;application/json&#39;
  6. },
  7. body: JSON.stringify({
  8. someData: someData,
  9. })
  10. })
  11. .then(response =&gt;
  12. response.text()
  13. )
  14. .then(response =&gt;{
  15. console.log(response);
  16. });

This is my html

  1. &lt;form action=&quot;&quot; name=&quot;getData&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
  2. &lt;button type = &quot;submit&quot; name = &quot;Download&quot;&gt;Download&lt;/button&gt;
  3. &lt;/form&gt;

答案1

得分: 4

return send_file(f, mimetype='application/msword', as_attachment=True, download_name='output.doc')

英文:

You need to specify the mimetype, It tries to detect the mimetype from the filename but since we are not saving it we need to specify the mimetype.

return send_file(f, mimetype=&#39;application/msword&#39;, as_attachment=True, download_name=&#39;output.doc&#39;)

huangapple
  • 本文由 发表于 2023年2月18日 00:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定