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

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

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:

document = Document()
document.add_heading("Some head-title")
document.add_paragraph('Some text')
f = BytesIO()
document.save(f)
f.seek(0)
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.

fetch('/getData', {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        someData: someData,
    })
})
.then(response => 
    response.text()
)
.then(response => {
    console.log(response);
});

This is my HTML:

<form action="" name="getData" method="post" enctype="multipart/form-data">
    <button type="submit" name="Download">Download</button>
</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:

document = Document()
document.add_heading(&quot;Some head-title&quot;)
document.add_paragraph(&#39;Some text&#39;)
f = BytesIO()
document.save(f)
f.seek(0)
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.

fetch(&#39;/getData&#39;, {
    method : &#39;POST&#39;,
    headers: {
        &#39;Accept&#39;: &#39;application/json&#39;,
        &#39;Content-Type&#39;: &#39;application/json&#39;
    },
    body: JSON.stringify({
        someData: someData,
    })
})
.then(response =&gt; 
    response.text()
)
.then(response =&gt;{
    console.log(response);
});

This is my html

&lt;form action=&quot;&quot; name=&quot;getData&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
&lt;button type = &quot;submit&quot; name = &quot;Download&quot;&gt;Download&lt;/button&gt;
&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:

确定