Skip to content

Instantly share code, notes, and snippets.

@m760622
Forked from bantic/upload.swift
Created September 3, 2018 21:28
Show Gist options
  • Save m760622/ee5ad932ed94ce312488acd9231d2669 to your computer and use it in GitHub Desktop.
Save m760622/ee5ad932ed94ce312488acd9231d2669 to your computer and use it in GitHub Desktop.
upload UIImage to server with swift
func saveImage(image: UIImage, name:String) {
let req = NSMutableURLRequest(url: NSURL(string:"http://127.0.0.1:3001/")! as URL)
let ses = URLSession.shared
req.httpMethod="POST"
req.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
req.setValue(name, forHTTPHeaderField: "X-FileName")
let jpgData = UIImageJPEGRepresentation(image, 1.0)
req.httpBodyStream = InputStream(data: jpgData!)
let task = ses.uploadTask(withStreamedRequest: req as URLRequest)
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment