Sample code for 30+ languages & platforms
Swift

Example: Http.DownloadAppend method

Demonstrates the DownloadAppend method.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    success = false

    var targetPath: String? = "c:/temp/qa_output/download.txt"

    let http = CkoHttp()!
    http.keepResponseBody = true

    // Assume the target file in the local filesystem does not yet exist.
    success = http.downloadAppend(url: "https://chilkatsoft.com/testData/helloWorld.txt", appendToPath: targetPath)
    var statusCode: Int = http.lastStatus.intValue
    if statusCode == 0 {
        // Unable to either send the request or get the response.
        print("\(http.lastErrorText!)")
        return
    }

    // Should be 200.
    print("Response status code: \(statusCode)")

    // Examine the contents of the file.
    let sb = CkoStringBuilder()!
    sb.loadFile(path: targetPath, charset: "utf-8")
    print("\(sb.getAsString()!)")

    // Output: 
    // Response status code: 200
    // Hello World!

    // Download another text file and append to the target file.
    success = http.downloadAppend(url: "https://chilkatsoft.com/testData/this_is_a_test.txt", appendToPath: targetPath)
    statusCode = http.lastStatus.intValue
    if statusCode == 0 {
        // Unable to either send the request or get the response.
        print("\(http.lastErrorText!)")
        return
    }

    // Should be 200.
    print("Response status code: \(statusCode)")

    // Examine the contents of the file.
    sb.loadFile(path: targetPath, charset: "utf-8")
    print("\(sb.getAsString()!)")

    // Output:
    // Response status code: 200
    // Hello World!This is a Test.

    // Delete the local target file.
    let fac = CkoFileAccess()!
    fac.fileDelete(path: targetPath)

    // Try to download a file that does not exist:
    success = http.downloadAppend(url: "https://chilkatsoft.com/testData/does_not_exist.txt", appendToPath: targetPath)
    statusCode = http.lastStatus.intValue
    if statusCode == 0 {
        // Unable to either send the request or get the response.
        print("\(http.lastErrorText!)")
    }
    else {
        // We got a response, and we already know it wasn't a 200 success response.
        // It should be a 404 not found.
        print("Response status code: \(statusCode)")
        // Examine the response body.
        print("Response body:")
        print("\(http.lastResponseBody!)")
    }


}