Swift
Swift
HTTPS Upload File to Web Server
See more HTTP Examples
Uploads a file to a web server using HTTPS.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The ContentType, HttpVerb, and Path properties should
// always be explicitly set.
let req = CkoHttpRequest()!
req.httpVerb = "POST"
req.path = "/receiveMyUpload.aspx"
req.contentType = "multipart/form-data"
req.addString(forUpload: "fileA", filename: "fileA.txt", strData: "This is the contents of file A", charset: "utf-8")
success = req.addFile(forUpload: "starfish.jpg", path: "qa_data/jpg/starfish.jpg")
if success == false {
print("\(req.lastErrorText!)")
return
}
let http = CkoHttp()!
// ----------------------------------------------------------------------------
// IMPORTANT:
// HTTP uploads require a counterpart implementation on the server, written in any desired language
// such as C#, Classic ASP, PHP, etc., which consumes the upload being sent.
// See: ASP.NET Receive Upload
// ----------------------------------------------------------------------------
// Do the upload.
var useSslTls: Bool = true
let resp = CkoHttpResponse()!
success = http.httpSReq(domain: "www.example.com", port: 443, ssl: useSslTls, request: req, response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
print("response status code = \(resp.statusCode.intValue)")
print("response body:")
print("\(resp.bodyStr!)")
}