Upload files with custom delegate

import Cocoa

class uploadToServer: NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate {

    var response: URLResponse?
    var session: URLSession?
    var uploadTask: URLSessionUploadTask?
    let opQueue = OperationQueue()
    
    func upload() {
        let config = URLSessionConfiguration.default
        let url = URL(string: "http://localhost:8042/instances")
        let request = NSMutableURLRequest(url: url!)
        request.httpMethod = "POST"
        request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
        let folder = Bundle.main.path(forResource: "dicom", ofType: "")!         
        for i in 1...10 {  
            let fileurl = URL(string: folder + "/" + String(i) + ".dcm")
            session = URLSession(configuration: config, delegate: self, delegateQueue: self.opQueue)
            if let task = session?.uploadTask(with: request as URLRequest, fromFile: fileurl! as URL) {
                task.resume()
            }  // end if
        }  // end for
    }  // end func
    
    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        // the task finished
        if let err = error {
            print("Error: \(err.localizedDescription)")
        } else {
            print("The upload was successful.")
            self.session?.finishTasksAndInvalidate()
        }  // end if
    }  // end func
    
    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
        print("did receive response")
        self.response = response
        print(self.response!)
        completionHandler(URLSession.ResponseDisposition.allow)
    }  // end func
    
    func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
        let progress = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
        print(progress)
    }  // end func
    
}  // end class