# Flask 文件流下载

```python
from flask import jsonify, json, request, make_response, Response, stream_with_context, send_file
import mimetypes,os

@api.route("/downloadFile/<path:filename>")
def downloadFile(filename):
    #baseDir = os.path.abspath(os.path.dirname(__file__)).split('applications')[0]
    #pathname = os.path.join(baseDir, "static/public/" + filename)
    baseDir = os.path.join(os.getcwd(), "static")
    pathname = os.path.join(baseDir, filename)
    print pathname
    def send_chunk():  # 流式读取
        store_path = pathname
        with open(store_path, 'rb') as target_file:
            while True:
                chunk = target_file.read(20 * 1024 * 1024)  # 每次读取20M
                if not chunk:
                    break
                yield chunk


    response = Response(send_chunk(), content_type='application/octet-stream')
    response.headers['Content-Disposition'] = 'attachment; filename={}'.format(filename)
    return response


@api.route("/downloadFile2/<path:filename>")
def downloadFile2(filename):
    import os
    baseDir = os.path.join(os.getcwd(), "static")
    pathname = os.path.join(baseDir, filename)
    f = open(pathname, "rb")
    response = Response(f.readlines())
    # response = make_response(f.read())
    mime_type = mimetypes.guess_type(filename)[0]
    response.headers['Content-Type'] = mime_type
    response.headers['Content-Disposition'] = 'attachment; filename={}'.format(filename.encode().decode('latin-1'))
    return response
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/python/flask/flask-wen-jian-liu-xia-zai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
