将图像从iOS设备上传到Flask服务器

时间:2016-04-28 15:05:32

标签: swift flask image-uploading

我正在编写一个应用程序,其功能是通过API将图像上传到服务器。这是我的提交功能:

@IBAction func submit(){
    print("submit")
    if(imageView.image == nil){
        print("No image")
    }else{
        let imageData = UIImageJPEGRepresentation(imageView.image!, 0.5)
        Alamofire.upload(
            .POST,
            "http://<ipaddress of server>/post_picture_to_id/"+item.id+"&image.jpg",
            multipartFormData: { multipartFormData in
                multipartFormData.appendBodyPart(data: imageData!, name: "files",
                    fileName: "image.jpg", mimeType: "image/jpeg")
            },
            encodingCompletion: { encodingResult in }
        )
    }

}

这是我的服务器端代码,用于接收文件。

@app.route('/post_picture_to_id/<string:id>&<string:upload_filename>', methods=['POST'])
    file = request.files[upload_filename]
    if file and allowed_file(file.filename):
        /* do things with the file */
    return make_response(dumps("upload successful"), 200)
return make_response(dumps("upload unsuccessful"), 400)

我尝试按

上传文件
r=requests.post('http://<serverip>/post_picture_to_id/57002008cd2ef6359d7f7a05&alderman.png', files={'alderman.png': open('alderman.png', 'rb')})

这是有效的。任何人都可以告诉我为什么它不能在Swift上工作?

0 个答案:

没有答案