简体   繁体   中英

Access encodingResult when uploading with Alamofire 5

I'm trying to update my app to Alamofire 5 and having difficulties due to a hack-ish way I'm using it I guess.

Anyhow, I need background uploads and Alamofire is not really designed to do this. Even so, I was using it to create a properly formatted file containing multipart form so I can give it to the OS to upload in the background later.

I'll post the code doing this in Alamofire 4, my question is how can I get the url of the file I was previously getting with encodingResults?

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

Multipart encoding is fully integrated into the now-asynchronous request pipeline in Alamofire 5. That means there's no separate step to use. However, you can use the MultipartFormData type directly, just like you would in the request closure.

let data = MultipartFormData()
data.append(Data(), withName: "dataName")
try data.encode()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM