Flask-Angular上传问题

时间:2019-02-08 05:23:49

标签: angularjs flask

对于我的项目,我在服务器端使用Flask,在客户端使用Angular。当我调用flask API上载CSV文件时,有时文件会成功上传,有时会在文件名上抛出keyError。 我一直在面对这个问题,并在调用API时尝试了各种方法来处理标头,但是这样做并不算幸运。 我已经联系了Flask社区,但他们说这是客户端的一些错误。 我也联系了角社区,但他们说不 这与表单数据从客户端(角度)发送到服务器端并且未正确传递有关。

这是我的烧瓶代码

        new_dataset_flag = 0
    file = request.files['trainingData']

这是我的角度代码:-

export class TrainingComponent implements OnInit {


 @ViewChild('trainingData') fileInput:ElementRef;

这就是我调用后端api的方式:-

const fileBrowser = this.fileInput.nativeElement;
    let temp = [];
    if (fileBrowser.files && fileBrowser.files[0]) {
      this.loading = true;
      const formData = new FormData();
      formData.append('trainingData', fileBrowser.files[0]);
      this.httpClient.post('http://127.0.0.1:5001/uploadXYZ/formData'

这是我在后端获得的追溯

Traceback (most recent call last):
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_restful\__init__.py", line 273, in error_router
    return original_handler(e)
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_restful\__init__.py", line 273, in error_router
    return original_handler(e)
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\6-feb\intent-python\src\intent\incident\incidenttraining.py", line 20, in db_insert_training_tickets
    return IncidentTraining.db_insert_training_tickets(customer_id, dataset_name, team_name)
  File "D:\6-feb\intent-python\src\intent\incident\incidenttraining.py", line 41, in db_insert_training_tickets
    file = request.files['trainingData']
  File "C:\Users\deepak.saini\AppData\Local\Programs\Python\Python36\lib\site-packages\werkzeug\datastructures.py", line 431, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'trainingData'
Error in atexit._run_exitfuncs:

0 个答案:

没有答案