1

I have an upload method using multipart in my current app which is working, however, I are using Alamofire 4 for the Get and Post methods and I'd like to use the multipart ability of Alamofire to upload the files so that I can utilise the completion results in the UI.

But I can't convert our current code to use Alamofire successfully.

Essentially the current code creates the entire multipart format including custom boundaries and fire it all into the URL body. But I don't seem to be able to recreate this in Alamofire?

My current code is as follows:

    pollServer() { (liveServer) -> () in

    let boundary = "----------Boundary\(sUUID)"
    let methodURL : String = String(format: "https://\(liveServer)/\(methodName)")


    // create url request to send
    var mutableURLRequest = NSMutableURLRequest(url: NSURL(string: methodURL)! as URL)
    mutableURLRequest.httpMethod = Alamofire.HTTPMethod.post.rawValue
    let boundaryConstant = boundary
    let contentType = "multipart/form-data;boundary="+boundaryConstant
    mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
    mutableURLRequest.httpShouldHandleCookies = false
    mutableURLRequest.timeoutInterval = 30
    mutableURLRequest.cachePolicy = .reloadIgnoringLocalCacheData
    mutableURLRequest.httpMethod = "POST"
    //-- Append data into posr url using following method

    let uploadData = NSMutableData()

    //-- "sUUID" is keyword from service
    uploadData.append("--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)
    uploadData.append("Content-Disposition: form-data; name=\"sUUID\"\r\n\r\n".data(using: String.Encoding.utf8)!)
    uploadData.append("\(sUUID)".data(using: String.Encoding.utf8)!)
    uploadData.append("\r\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)
    //-- For sending image into service if needed (send image as imagedata)
    uploadData.append("Content-Disposition: form-data; name=\"files[]\"; filename=\"\(fileName)\"\r\n".data(using: String.Encoding.utf8)!)
    uploadData.append("Content-Type: application/octet-stream\r\n\r\n".data(using: String.Encoding.utf8)!)
    uploadData.append(sStream)
    //-- "sEvent" is keyword form service
    uploadData.append("\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)
    uploadData.append("Content-Disposition: form-data; name=\"nEventID\"\r\n\r\n".data(using: String.Encoding.utf8)!)
    uploadData.append("\(EventID)".data(using: String.Encoding.utf8)!)
    //-- "sExt" is keyword form service
    uploadData.append("\r\n--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
    uploadData.append("Content-Disposition: form-data; name=\"sExt\"\r\n\r\n".data(using: String.Encoding.utf8)!)
    uploadData.append("\(sExt)".data(using: String.Encoding.utf8)!)
    uploadData.append("\r\n--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)

    mutableURLRequest.httpBody = uploadData as Data
    let postLength = "\(UInt(uploadData.length))"
    mutableURLRequest.setValue(postLength, forHTTPHeaderField: "Content-Length")
    mutableURLRequest.url = NSURL(string: methodURL) as URL?

        let session = URLSession.shared

        let task = session.dataTask(with: mutableURLRequest as URLRequest, completionHandler: {data, response, error -> Void in
            print("Response: \(response)")
            print("Data: \(data?.description)")
            print("Error: \(error?.localizedDescription)")
        }

        )

        task.resume()

    }
Tony Law
  • 293
  • 2
  • 13

0 Answers0