Skip to content

Instantly share code, notes, and snippets.

@bitomule
Created March 12, 2015 11:43
Show Gist options
  • Save bitomule/6fe5563e43031c06de3b to your computer and use it in GitHub Desktop.
Save bitomule/6fe5563e43031c06de3b to your computer and use it in GitHub Desktop.
UploadToMeteor Controller
class UploadViewController: BaseViewController,NSURLSessionDelegate,NSURLSessionTaskDelegate,NSURLSessionDataDelegate {
var file:File?
var type:String = ""
var responseData = NSMutableData()
override func viewDidLoad() {
super.viewDidLoad()
let path = file!.getDownloadedPath()
var data: NSData = NSData(contentsOfURL: path)!
type = mimeTypeForPath(path.absoluteString!)
var request = NSMutableURLRequest(URL: NSURL(string: "http://" + MeteorAgent.appUrl() + "/appUploadFile")!)
request.HTTPMethod = "POST"
request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
request.setValue(file!.name!, forHTTPHeaderField: "FileData-name")
request.setValue(type, forHTTPHeaderField: "FileData-type")
request.HTTPBodyStream = NSInputStream(data: data)
uploadFile(request,data: data)
}
func uploadFile(request:NSURLRequest,data:NSData){
var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
var task = session.uploadTaskWithRequest(request, fromData: data)
task.resume()
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if error != nil{
println(error?.localizedDescription)
}else{
println("uploaded")
}
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
var uploadProgress:Double = Double(totalBytesSent)/Double(totalBytesExpectedToSend)
println(uploadProgress)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
completionHandler(NSURLSessionResponseDisposition.Allow)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
responseData.appendData(data)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, needNewBodyStream completionHandler: (NSInputStream!) -> Void) {
let path = file!.getDownloadedPath()
var data: NSData = NSData(contentsOfURL: path)!
completionHandler(NSInputStream(data: data))
}
func mimeTypeForPath(path: String) -> String {
let pathExtension = path.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as NSString
}
}
return "application/octet-stream";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment