gpt4 book ai didi

ios - 带 header 的Alamofire 4分段上传

转载 作者:行者123 更新时间:2023-12-01 20:00:00 25 4
gpt4 key购买 nike

如何使用Alamofire v4用自己的标题上传?
在v3的情况下,我为此编写了自己的函数,例如以下代码,但是对于v4我不能做同样的事情。

static func upload(method: Alamofire.Method, urlString: String, multipartFormData: (data: Alamofire.MultipartFormData) -> (), completionHandler: (
response: Response<AnyObject, NSError>, statusCode:Int, json:JSON) -> ()){
Alamofire.upload(method, urlString, headers: MyRequest.makeHeaders(), multipartFormData: multipartFormData, encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
switch response.result {
case .Failure:
log.error("Upload request failed!!")
case .Success:
completionHandler(response: response, statusCode: (response.response?.statusCode)!, json:JSON(response.result.value!))
}
}
case .Failure(let error):
log.warning((error as NSError).localizedDescription)
}
})
}

我尝试了以下代码,但没有用
static func upload(_ method: Alamofire.HTTPMethod, urlString: String, multipartFormData: (Alamofire.MultipartFormData) -> Swift.Void, completionHandler: @escaping
(_ response: NSDictionary, _ statusCode:Int, _ json:JSON) -> Void){

Alamofire.upload(multipartFormData: multipartFormData, to: urlString, method: method, headers: MyRequest.makeHeaders(), encodingCompletion: {
encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
switch response.result {
case .failure:
log.error("Upload request failed!!")
case .success:
completionHandler(response: response, statusCode: (response.response?.statusCode)!, json:JSON(response.result.value!))
}
}
case .failure(let error):
log.warning((error as NSError).localizedDescription)
}
})

}

最佳答案

试试下面的代码:

Alamofire.upload(multipartFormData:{ multipartFormData in
multipartFormData.append(unicornImageURL, withName: "unicorn")
multipartFormData.append(rainbowImageURL, withName: "rainbow")},
usingThreshold:UInt64.init(),
to:"https://httpbin.org/post",
method:.post,
headers:["Authorization": "auth_token"],
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})

关于ios - 带 header 的Alamofire 4分段上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com