【问题标题】:Pass an uploaded image directly from json file直接从 json 文件传递​​上传的图像
【发布时间】:2017-05-25 07:46:32
【问题描述】:

我正在尝试将图像从我的应用上传到我的 heroku 后端,然后将其传递给 Stripe 进行验证。这是我上传和传递图像的快速代码:

@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)
    }
}

为了便于阅读,我删除了上面的参数列表并留下了一个。

有没有办法接收这个文件并将其上传到条带,而不必通过传递文件参数来保存它?像这样:

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 backendStripe::FileUpload.create 是否会为我上传到条带?

任何关于这两者的见解都将非常感谢。

【问题讨论】:

    标签: swift swift3 stripe-payments


    【解决方案1】:

    您需要先使用 "create file upload" 调用将文件上传到 Stripe 的 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 应用的代码执行相同的操作。基本上,您需要做的就是向https://uploads.stripe.com/v1/files 发送一个带有Authorization 标头的POST 请求,其值为Bearer pk_...(其中pk_... 是您可发布的API 密钥)并输入multipart/form-data,文件的内容作为请求的身体。这篇博文应该有助于使用 Swift 发送 multipart/form-data 请求:http://www.kaleidosblog.com/how-to-upload-images-using-swift-2-send-multipart-post-request

    【讨论】:

    【解决方案2】:

    感谢@Ywain,我能够为 IOS Swift 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)")
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 2020-05-06
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多