0

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
                        }
                    }
                )
Nemanja Kovacevic
  • 3,510
  • 2
  • 30
  • 45

1 Answers1

1

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()
Jon Shier
  • 12,200
  • 3
  • 35
  • 37
  • Thanks Jon, this is exactly what I needed and what I should have used in the first place to create a multipart form data file instead of attempting a bogus AF request and than trying to grab multipart form data file before AF deletes it... – Nemanja Kovacevic Jun 18 '20 at 16:52