2017-03-27 47 views
0

我试图从客户端(Ionic框架)发送图像到我的Python后端(烧瓶)。出于某种原因,我不断收到错误代码3,我知道这是一个连接错误,但我不知道为什么。发送图像到Python后端 - 科尔多瓦文件传输

客户端代码(AngularJS):

$scope.submit = function() { 
     if($scope.details.title === '' || $scope.details.price === '' || $scope.details.description === '' 
     || $scope.details.category === '') { 
      ionicSuperPopup.show("Error!", "Fill out the appropriate fields!", "error"); 
     } else if($scope.size !== 2) { 
      ionicSuperPopup.show("Error!", "You need to upload at least two images.", "error"); 
     } else { 
      /* Post item to server, wait for success, and present success post. */ 
      //ItemFactory.addItem($scope.details); 
      var url = 'http://127.0.0.1:5000/api/item/addItem'; 
      var name = $scope.details.images[0]; 
      var options = new FileUploadOptions(); 
      options.fileKey = 'image', 
      options.fileName = name, 
      options.chunkedMode = false, 
      options.mimeType = 'image/jpg', 
      options.params = { 'filename': name }; 
      options.headers = { 
       Connection: "close" 
      }; 

      var ft = new FileTransfer(); 
      ft.upload($scope.list[0], encodeURI(url), win, fail, options); 
     } /* End of 'else' */ 
    } /* End of 'submit' */ 

$ scope.list [0]包含拍摄的照片的imageURI。

后端代码:

from flask_restful import reqparse, Resource 

    class Item(Resource): 
     parser = reqparse.RequestParser(bundle_errors=True) #All arguments must be present 

     def post(self): 
      Item.parser.add_argument('image', required=True, help="You need images.") 
      args = Item.parser.parse_args() 
      print("We have: {}".format(args)) 

有什么,我做错了吗?我觉得我已经覆盖了一切,但我不明白为什么我有一个错误代码3.感谢您的帮助!

+0

只是好奇,你为什么要使用连接关闭HTTP标头? –

+0

这是一个试图解决这个问题。但我实际上找出了什么是错的。您需要: Item.parser.add_argument('file',required = True,help =“您需要图像。”,type = werkzeug.datastructures.FileStorage,location ='files') – Zain

回答

0

所以我真的找出了这个问题。图像需要加载的文件上传和我用WERKZEUG的FileStorage做到这一点:

Item.parser.add_argument('file', required=True, help="You need image.", type=werkzeug.datastructures.FileStorage, location='files') 
相关问题