简体   繁体   English

Flask request.files始终为空

[英]Flask request.files is always empty

I am making a POST request to send a JSON object with keys containing files. 我正在发出一个POST请求来发送一个包含文件的密钥的JSON对象。 An example of what I send to the backend is: 我发送到后端的一个例子是:

export interface PosInputFiles {
org: string;
year_month: string;
in_master_file: File;
iv_file?: File;
sales_file?: File;
recv_file?: File;
transfer_file?: File;
adjust_file?: File;
pcount_file?: File;
gift_file?: File;
xrate?: string;

} }

My POST request looks like: 我的POST请求如下:

  generateMaster(args: PosInputFiles) {
return this.http.post('http://localhost:5000/api', args, { headers: this.headers});

} }

When I try to access these files from request.json, the values are an empty dict ({}). 当我尝试从request.json访问这些文件时,值为空dict({})。

try:
        org = request.json['org']
        year_month = request.json['year_month']
        in_master_file = request.json['in_master_file']
        iv_file = None if 'iv_file' not in request.json else request.json['iv_file']
        sales_file = None if 'sales_file' not in request.json else request.json['sales_file']
        recv_file = None if 'recv_file' not in request.json else request.json['recv_file']
        transfer_file = None if 'transfer_file' not in request.json else request.json['transfer_file']
        adjust_file = None if 'adjust_file' not in request.json else request.json['adjust_file']
        pcount_file = None if 'pcount_file' not in request.json else request.json['pcount_file']
        gift_file = None if 'gift_file' not in request.json else request.json['gift_file']
        xrate = None if 'xrate' not in request.json else request.json['xrate']
    except:
        return { "post" : "failed" }

    print(in_master_file)
    print(len(request.files))

    return { "post": "success"}

Then I tried sending only one file and made sure len(request.json) == 0 through POSTMan and my frontend (Angular8). 然后我尝试只发送一个文件并通过POSTMan和我的前端(Angular8)确保len(request.json)== 0。 However, len(request.files) is also 0 and every time I try to access something, there is 400 error. 但是,len(request.files)也是0,每次我尝试访问某些东西时,都会有400错误。 My POST request is successful as I always print {"post", "success"} but for some reason, no files make it to the backend. 我的POST请求成功,因为我总是打印{“post”,“success”}但由于某种原因,没有文件进入后端。 All my files sent are real files and I have made sure that I am sending the file. 我发送的所有文件都是真实文件,我确保发送文件。 Thank you so much for your help! 非常感谢你的帮助!

For those who might have the same problem eventually, here's how I solved this issue. 对于那些最终可能遇到同样问题的人来说,这就是我解决这个问题的方法。 Flask doesn't recognize files that aren't of FormData type so that's why I could only access JSON. Flask无法识别不属于FormData类型的文件,因此我只能访问JSON。 Thus, I had to append all my files to a FormData variable. 因此,我不得不将我的所有文件追加到FormData变量。

generateMaster(submittedFiles: PosInputFiles) {
const formData: FormData = new FormData();
Object.keys(submittedFiles).forEach(key => {
  if (submittedFiles[key] instanceof File) {
    formData.append(key, submittedFiles[key], submittedFiles[key].name);
  } else {
    formData.append(key, new File([], submittedFiles[key]), submittedFiles[key].name);
  }
})
return this.http.post('http://localhost:5000/api', formData, { headers: this.headers});

} }

Then backend will finally recognize the files. 然后后端将最终识别文件。 In order to get other string data, and floats, I stored those values as the filename and could access them as such. 为了获得其他字符串数据和浮点数,我将这些值存储为文件名,并可以这样访问它们。

 def post(self):
    # Initilize arguments
    org = request.files.get('org').filename
    year_month = request.files.get('year_month').filename
    in_master_file = request.files.get('in_master_file')
    iv_file = request.files.get('iv_file')
    sales_file = request.files.get('sales_file')
    ...
    xrate = None if 'xrate' not in request.files else float(request.files.get('xrate').filename)

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

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