2017-03-27 73 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') 
相關問題