0
votes

I have been trying to upload a video to my local server using the following uploading approach:

Alamofire.upload(
        multipartFormData: { multipartFormData in

            multipartFormData.append( url, withName: "video", fileName: "video.mp4", mimeType: "video/mp4" )

        },
        with: Router.postVideo( parameters: params ),
        encodingCompletion: { encodingResult in

            switch encodingResult
            {

                case .success( let upload, _, _ ):

                    upload.uploadProgress { progress in
                        print( progress.fractionCompleted )
                    }

                    upload.responseJSON { response in
                        debugPrint(response)
                    }

                case .failure( let encodingError ):
                    print(encodingError)

            }

        })

Instead of appending parameters in the multipartFormData block, I making use of the following `Router'.

enum APIRouter: URLRequestConvertible
{
    static let baseURLString = "https://base/api/"

    case postVideo( parameters: Parameters )

    var method: HTTPMethod
    {
        switch self
        {
            case .postVideo:
                return .post
            ...
        }
    }

   var parameters: Parameters?
   {
        switch self
        {
            case .postVideo( let parameters ):
                return parameters
            ....
        }
    }

    var path: String
    {
       switch self
       {
            case .postVideo:
                return “videos"
            ....  
        }
    }

    public func asURLRequest() throws -> URLRequest
    {
        // Create url request
        let url = try APIRouter.baseURLString.asURL()
        var urlRequest = URLRequest( url: url.appendingPathComponent( path ) )

        // Set request method
        urlRequest.httpMethod = method.rawValue

        // Set parameters
        if let params = self.parameters
        {
            urlRequest = try URLEncoding.default.encode( urlRequest, with: params )
        }

        return urlRequest
    }
}

However, when I upload the video to the server, the parameters are missing. Any ideas why?

1

1 Answers

0
votes

I know the question is a bit old, but I had the same problem recently and I solved it by not using the default URLEncoding and instead creating a new one like this:

URLEncoding(destination: .queryString) or URLEncoding.queryString for short

This works with Alamofire 4.x. I hope this helps.

For more information you can also read this.