Skip to content

Instantly share code, notes, and snippets.

@yoshimax
Created May 30, 2017 20:36
Show Gist options
  • Save yoshimax/ae232bb5fe7b9628d53033f70a42f8aa to your computer and use it in GitHub Desktop.
Save yoshimax/ae232bb5fe7b9628d53033f70a42f8aa to your computer and use it in GitHub Desktop.
Upload to FlashAir from macApp via Alamofire
// upload to FlashAir from macosApp via Alamofire
import Alamofire
class ViewController: NSViewController {
@IBAction func GetFileSelectClicked(_ sender: Any) {
let file: NSOpenPanel = NSOpenPanel()
file.allowsMultipleSelection = false
file.canChooseFiles = true
file.canCreateDirectories = true
file.allowedFileTypes = ["mp4","mov","m4v","png"]
file.runModal()
let openfile = file.url
if(openfile != nil) {
print(openfile)
fileUpload(openfile!)
}
}
// App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
// note: info.plist App Transport Security Settings > Allow Arbitrary Loads = YES
func fileUpload(_ url:URL) {
let postUrl = "http://flashair.local/" + "upload.cgi"
Alamofire.upload(multipartFormData:{ multipartFormData in
multipartFormData.append(url, withName: "file")
},
usingThreshold:UInt64.init(),
to:postUrl,
method:.post,
headers:["Authorization": ""],
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
print("sucess")
case .failure(let encodingError):
print("Eror")
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment