简体   繁体   English

如何使用 Flask test_client 上传多个文件?

[英]How do I upload multiple files using the Flask test_client?

How can I use the Flask test_client to upload multiple files to one API endpoint?如何使用 Flask test_client 将多个文件上传到一个 API 端点?

I'm trying to use the Flask test_client to upload multiple files to a web service that accepts multiple files to combine them into one large file.我正在尝试使用 Flask test_client 将多个文件上传到接受多个文件以将它们组合成一个大文件的 Web 服务。

My controller looks like this:我的控制器看起来像这样:

@app.route("/combine/file", methods=["POST"])
@flask_login.login_required
def combine_files():

  user = flask_login.current_user

  combined_file_name = request.form.get("file_name")

  # Store file locally
  file_infos = []
  for file_data in request.files.getlist('file[]'):

    # Get the content of the file
    file_temp_path="/tmp/{}-request.csv".format(file_id)
    file_data.save(file_temp_path)

    # Create a namedtuple with information about the file
    FileInfo = namedtuple("FileInfo", ["id", "name", "path"])
    file_infos.append(
      FileInfo(
        id=file_id,
        name=file_data.filename,
        path=file_temp_path
      )
    )
    ...

My test code looks like this:我的测试代码如下所示:

def test_combine_file(get_project_files):

project = get_project_files["project"]

r = web_client.post(
    "/combine/file",
    content_type='multipart/form-data',
    buffered=True,
    follow_redirects=True,
    data={
        "project_id": project.project_id,
        "file_name": "API Test Combined File",
        "file": [
            (open("data/CC-Th0-MolsPerCell.csv", "rb"), "CC-Th0-MolsPerCell.csv"),
            (open("data/CC-Th1-MolsPerCell.csv", "rb"), "CC-Th1-MolsPerCell.csv")
]})
response_data = json.loads(r.data)

assert "status" in response_data
assert response_data["status"] == "OK"

However, I can't get the test_client to actually upload both files.但是,我无法让 test_client 实际上传这两个文件。 With more than one file specified, the file_data is empty when the API code loops.如果指定了多个文件,则 API 代码循环时 file_data 为空。 I have tried my own ImmutableDict with two "file" entries, a list of file tuples, a tuple of file tuples, anything I could think of.我已经尝试了我自己的 ImmutableDict 有两个“文件”条目,一个文件元组列表,一个文件元组元组,我能想到的任何东西。

What is the API to specify multiple files for upload in the Flask test_client? Flask test_client 中指定多个文件上传的API是什么? I can't find this anywhere on the web!我在网络上的任何地方都找不到这个! :( :(

The test client takes a list of file objects (as returned by open() ), so this is the testing utility I use:测试客户端获取文件对象列表(由open()返回),所以这是我使用的测试实用程序:

def multi_file_upload(test_client, src_file_paths, dest_folder):
    files = []
    try:
        files = [open(fpath, 'rb') for fpath in src_file_paths]
        return test_client.post('/api/upload/', data={
            'files': files,
            'dest': dest_folder
        })
    finally:
        for fp in files:
            fp.close()

I think if you lose your tuples (but keeping the open() s) then your code might work.我认为如果您丢失了元组(但保留了open() s),那么您的代码可能会起作用。

Another way of doing this- if you want to explicitly name your file uploads here (my use case was for two CSVs, but could be anything) with test_client is like this:这样做的另一种方法 - 如果你想在这里明确命名你的文件上传(我的用例是两个 CSV,但可以是任何东西)与 test_client 是这样的:

   resp = test_client.post(
                           '/data_upload_api', # flask route
                           file_upload_one=[open(FILE_PATH, 'rb')],
                           file_upload_two=[open(FILE_PATH_2, 'rb')]
                           )

Using this syntax, these files would be accessible as:使用这种语法,这些文件可以通过以下方式访问:

request.files['file_upload_one'] # etc.

You should just send data object with your files named as you want:您应该只发送带有您想要命名的文件的数据对象:

test_client.post('/api/upload', 
                 data={'title': 'upload sample', 
                       'file1': (io.BytesIO(b'get something'), 'file1'), 
                       'file2': (io.BytesIO(b'forthright'), 'file2')},  
                 content_type='multipart/form-data')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM