将图像发送到Python后端 - Cordova文件传输

时间:2017-03-27 22:04:27

标签: python angularjs ionic-framework flask cordova-plugins

我试图将图像从客户端(Ionic Framework)发送到我的Python后端(Flask)。出于某种原因,我不断收到错误代码3,我理解这是一个连接错误,但我不确定原因。

客户端代码(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.感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

所以我实际上已经找到了问题所在。图像需要作为文件上传加载,我使用werkzeug的FileStorage来执行此操作:

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