直接从 json 文件传递上传的图片
Pass an uploaded image directly from json file
我正在尝试将我的应用程序中的图像上传到我的 heroku 后端,然后将其传递给 Stripe 进行验证。这是我的 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)
}
}
为了便于阅读,我删除了上面的参数列表并留下了一个。
有没有一种方法可以接收这个文件并将其上传到 stripe 而不必通过传递 file 参数来保存它?像这样:
Stripe::FileUpload.create(
{
:purpose => 'identity_document',
:file => params[file]
},
{:stripe_account => params[stripe_account]}
)
同样在 stripe 文档中,它说要将文件上传到“https://uploads.stripe.com/v1/files' but then shows code to put in your backend”,Stripe::FileUpload.create
会为我上传到 stripe 吗?
如有任何见解,将不胜感激。
您需要先使用 "create file upload" call. You can then use the file upload's ID (file_...
) to update the account's legal_entity.verification.document
属性将文件上传到 Stripe 的 API。
(这里解释这个过程:
- https://stripe.com/docs/connect/identity-verification#uploading-a-file
- https://stripe.com/docs/connect/identity-verification#attaching-the-file)
由于文件由用户提供,创建文件上传有两种选择:
让您的应用将文件上传到您的后端服务器,然后在您的后端使用该文件 create the file upload
直接从应用程序创建文件上传(使用您的可发布 API 密钥),并将生成的 file_upload 的 ID (file_...
) 发送到你的后端
下面是使用 jQuery 创建文件上传 client-side 的示例:https://jsfiddle.net/captainapollo/d8yd3761/。
您可以从 iOS 应用的代码中执行相同的操作。基本上,您需要做的就是向 https://uploads.stripe.com/v1/files
发送一个 POST 请求,其中 Authorization
header 的值为 Bearer pk_...
(其中 pk_...
是您的可发布API 键)并键入 multipart/form-data
,文件内容作为请求的 body。此博客 post 应该有助于使用 Swift 发送 multipart/form-data
请求:http://www.kaleidosblog.com/how-to-upload-images-using-swift-2-send-multipart-post-request
感谢@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)")
}
}
我正在尝试将我的应用程序中的图像上传到我的 heroku 后端,然后将其传递给 Stripe 进行验证。这是我的 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)
}
}
为了便于阅读,我删除了上面的参数列表并留下了一个。
有没有一种方法可以接收这个文件并将其上传到 stripe 而不必通过传递 file 参数来保存它?像这样:
Stripe::FileUpload.create(
{
:purpose => 'identity_document',
:file => params[file]
},
{:stripe_account => params[stripe_account]}
)
同样在 stripe 文档中,它说要将文件上传到“https://uploads.stripe.com/v1/files' but then shows code to put in your backend”,Stripe::FileUpload.create
会为我上传到 stripe 吗?
如有任何见解,将不胜感激。
您需要先使用 "create file upload" call. You can then use the file upload's ID (file_...
) to update the account's legal_entity.verification.document
属性将文件上传到 Stripe 的 API。
(这里解释这个过程:
- https://stripe.com/docs/connect/identity-verification#uploading-a-file
- https://stripe.com/docs/connect/identity-verification#attaching-the-file)
由于文件由用户提供,创建文件上传有两种选择:
让您的应用将文件上传到您的后端服务器,然后在您的后端使用该文件 create the file upload
直接从应用程序创建文件上传(使用您的可发布 API 密钥),并将生成的 file_upload 的 ID (
file_...
) 发送到你的后端
下面是使用 jQuery 创建文件上传 client-side 的示例:https://jsfiddle.net/captainapollo/d8yd3761/。
您可以从 iOS 应用的代码中执行相同的操作。基本上,您需要做的就是向 https://uploads.stripe.com/v1/files
发送一个 POST 请求,其中 Authorization
header 的值为 Bearer pk_...
(其中 pk_...
是您的可发布API 键)并键入 multipart/form-data
,文件内容作为请求的 body。此博客 post 应该有助于使用 Swift 发送 multipart/form-data
请求:http://www.kaleidosblog.com/how-to-upload-images-using-swift-2-send-multipart-post-request
感谢@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)")
}
}