使用 Alamofire 5 上传时访问 encodingResult
Access encodingResult when uploading with Alamofire 5
我正在尝试将我的应用程序更新到 Alamofire 5,但我猜是由于我使用它的黑客方式而遇到困难。
无论如何,我需要后台上传,而 Alamofire 并非真正设计用于执行此操作。即便如此,我还是用它创建了一个包含多部分表单的格式正确的文件,这样我就可以把它交给 OS 稍后在后台上传。
我将 post 在 Alamofire 4 中执行此操作的代码,我的问题是如何获取我之前使用 encodingResults 获取的文件的 url?
// We're not actually going to upload photo via alamofire. It does not offer support for background uploads.
// Still we can use it to create a request and more importantly properly formatted file containing multipart form
Api.alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageData, withName: "photo[image]", fileName: filename, mimeType: "image/jpg")
},
to: "http://", // if we give it a real url sometimes alamofire will attempt the first upload. I don't want to let it get to our servers but it fails if I feed it ""
usingThreshold: UInt64(0), // force alamofire to always write to file no matter how small the payload is
method: .post,
headers: Api.requestHeaders,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let alamofireUploadTask, _, let url):
alamofireUploadTask.suspend()
defer { alamofireUploadTask.cancel() }
if let alamofireUploadFileUrl = url {
// we want to own the multipart file to avoid alamofire deleting it when we tell it to cancel its task
let fileUrl = ourFileUrl
do {
try FileManager.default.copyItem(at: alamofireUploadFileUrl, to: fileUrl)
// use the file we just created for a background upload
} catch {
}
}
case .failure:
// alamofire failed to encode the request file for some reason
}
}
)
多部分编码已完全集成到 Alamofire 5 中现在的异步请求管道中。这意味着无需使用单独的步骤。但是,您可以直接使用 MultipartFormData
类型,就像在请求闭包中一样。
let data = MultipartFormData()
data.append(Data(), withName: "dataName")
try data.encode()
我正在尝试将我的应用程序更新到 Alamofire 5,但我猜是由于我使用它的黑客方式而遇到困难。
无论如何,我需要后台上传,而 Alamofire 并非真正设计用于执行此操作。即便如此,我还是用它创建了一个包含多部分表单的格式正确的文件,这样我就可以把它交给 OS 稍后在后台上传。
我将 post 在 Alamofire 4 中执行此操作的代码,我的问题是如何获取我之前使用 encodingResults 获取的文件的 url?
// We're not actually going to upload photo via alamofire. It does not offer support for background uploads.
// Still we can use it to create a request and more importantly properly formatted file containing multipart form
Api.alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageData, withName: "photo[image]", fileName: filename, mimeType: "image/jpg")
},
to: "http://", // if we give it a real url sometimes alamofire will attempt the first upload. I don't want to let it get to our servers but it fails if I feed it ""
usingThreshold: UInt64(0), // force alamofire to always write to file no matter how small the payload is
method: .post,
headers: Api.requestHeaders,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let alamofireUploadTask, _, let url):
alamofireUploadTask.suspend()
defer { alamofireUploadTask.cancel() }
if let alamofireUploadFileUrl = url {
// we want to own the multipart file to avoid alamofire deleting it when we tell it to cancel its task
let fileUrl = ourFileUrl
do {
try FileManager.default.copyItem(at: alamofireUploadFileUrl, to: fileUrl)
// use the file we just created for a background upload
} catch {
}
}
case .failure:
// alamofire failed to encode the request file for some reason
}
}
)
多部分编码已完全集成到 Alamofire 5 中现在的异步请求管道中。这意味着无需使用单独的步骤。但是,您可以直接使用 MultipartFormData
类型,就像在请求闭包中一样。
let data = MultipartFormData()
data.append(Data(), withName: "dataName")
try data.encode()