直接从JSON文件传递一个上传的图片

问题描述 投票:1回答:2

我想从我的应用程序上传图片到我的Heroku的后端,然后传递到条纹进行验证。这里是我的SWIFT代码上传,并通过图像:

@IBAction func registerAccountButtonWasPressed(sender: UIButton) {
    let dob = self.dobTextField.text!.components(separatedBy: "/")
    let URL = "https://splitterstripeservertest.herokuapp.com/account/create"
    var imageData = UIImageJPEGRepresentation(photoID,1)
    let params = ["year": UInt(dob[2])! as UInt] as [String : Any]

    let manager = AFHTTPSessionManager()
    let operation = manager.post(URL, parameters: params, constructingBodyWith: { (formData: AFMultipartFormData!) -> Void in
        formData.appendPart(withFileData: imageData!, name: "file", fileName: "photoID.jpg", mimeType: "image/jpeg")
    }, success: { (operation, responseObject) -> Void in
        print(responseObject!)
    }) { (operation, error) -> Void in
        self.handleError(error as NSError)
    }
}

我已经删除了上述OUT参数,列表,并留下一个可读性。

有没有一种办法再接收这个文件,并将其上传到条纹,而无需通过传递文件参数保存呢?像这样:

Stripe::FileUpload.create(
  {
    :purpose => 'identity_document',
    :file => params[file]
  },
  {:stripe_account => params[stripe_account]}
)

另外,在条纹文档它说,该文件上传到“https://uploads.stripe.com/v1/filesbut then shows code to put in your backend,并Stripe::FileUpload.create做上传到条纹给我吗?

在任任何有识之士将是巨大的感谢。

swift swift3 stripe-payments
2个回答
3
投票

你需要使用"create file upload"调用首先把文件上传到条纹的API。然后,您可以使用文件上传的ID(file_...)到update the accountlegal_entity.verification.document属性。

(这个过程在此说明:

由于该文件是由用户提供的,你必须创建文件上传两种选择:

  • 有您的应用程序文件上传到后台服务器,然后在后端,使用该文件create the file upload
  • 创建文件(使用发布的API密钥)从应用程序直接上传,并发送所产生的file_upload的ID(file_...)到后端

下面是创建文件上传客户端,使用jQuery的例子:https://jsfiddle.net/captainapollo/d8yd3761/

你可以从你的iOS应用程序的代码做同样的事情。基本上所有你需要做的就是发送POST请求与价值https://uploads.stripe.com/v1/filesAuthorizationBearer pk_...(其中pk_...是你发布的API密钥),然后键入multipart/form-data,与作为请求的身体文件的内容。本博客文章应该使用雨燕发送multipart/form-data请求是有帮助的:http://www.kaleidosblog.com/how-to-upload-images-using-swift-2-send-multipart-post-request


0
投票

由于@Ywain我能想出这个解决方案IOS斯威夫特4.我创建的文件直接从应用程序上传和检索file_upload的ID发送给我的后端附加到Connect帐户。我通过导入Alamofire和SwiftyJSON这样做。

let heads = ["Authorization": "Bearer \(stripePublishableKey)"]
let imageData = image!.jpegData(compressionQuality: 1.0)
let fileName = "\(NSUUID().uuidString).jpeg"


 Alamofire.upload(multipartFormData: { multipart in
            multipart.append(imageData!, withName: "file", fileName: fileName, mimeType: "image/jpeg")
            multipart.append(("identity_document").data(using: .utf8)!, withName :"purpose")

        }, to: fileUrl!, method: .post, headers: heads) { encodingResult in
            switch encodingResult {
              case .success(let upload, _, _):
                upload.responseJSON { answer in

                    let value = JSON(answer.value!)
                    let FileID = value["id"].stringValue


                    // Use fileID and Connect Account ID to create params to send to backend.

                    let params: [String: Any] = [
                        "acct_id": ConnectID!,
                        "fileID": FileID,
                        ]

                    print("statusCode: \(String(describing: answer.response?.statusCode))")
                }

            case .failure(let encodingError):
                print("multipart upload encodingError: \(encodingError)")
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.