Search code examples
swiftalamofire

Uploading MultipartFormData with files array


I have some problems with uploading files using Alamofire.upload multipartFormData. I need to upload several files into one request name.

For example i use:

var filesURLs = [fileURL: NSURL]()    
....
Alamofire.upload(Method.POST, url,  headers: headers, multipartFormData: { (multipartFormData: MultipartFormData) in

            //add files
            filesURLs({ (url) -> () in
                multipartFormData.appendBodyPart(fileURL: url, name: "photo")
            })

            }, encodingCompletion: { (encodingResult: Manager.MultipartFormDataEncodingResult) in ......

Here you see, That i want to set several files with name "photo", but in fact i have only 1 file.

How can I sent array of files into one name?


Solution

  • Some of this depends on what backend server you use, but with something like PHP, you can create a file array just by appending [] to the name. That means your add files component becomes

    filesURLs({ (url) -> () in
        multipartFormData.appendBodyPart(fileURL: url, name: "photo[]")
    })