-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileUploader.swift
72 lines (59 loc) · 3.22 KB
/
FileUploader.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// FileUploader.swift
// IndoMagneto
//
// Created by Alexander Steinbrecher on 04/01/15.
// Copyright (c) 2015 Alexander Steinbrecher. All rights reserved.
//
import Foundation
public class FileUploader {
public typealias CompletionHandler = (obj:AnyObject?, success: Bool?) -> Void
public func nativeUpload (fileName: String, data: String) -> Void {
//let filePath = NSURL.init(fileURLWithPath: "/Users/asteinbr/swiftTest")!
let url:NSURL? = NSURL(string: "http://146.185.179.179/upload.php")
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
request.HTTPMethod = "POST"
// Set Content-Type in HTTP header.
let boundaryConstant = "*****" // This should be auto-generated.
let contentType = "multipart/form-data;boundary=" + boundaryConstant
//let fileName = filePath.path!.lastPathComponent
let mimeType = "text/csv"
let fieldName = "uploaded_file"
let connectionType = "Keep-Alive"
let enctype = "multipart/form-data"
request.setValue(connectionType, forHTTPHeaderField: "Connection")
request.setValue(enctype, forHTTPHeaderField: "ENCTYPE")
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.setValue(fileName, forHTTPHeaderField: "uploaded_file")
println(request.allHTTPHeaderFields)
// Set data
var error: NSError?
var dataString = "--\(boundaryConstant)\r\n"
dataString += "Content-Disposition: form-data; name=\"\(fieldName)\"; filename=\"\(fileName)\"\r\n"
dataString += "\n"
//dataString += "Content-Type: \(mimeType)\r\n\r\n"
//dataString += String(contentsOfFile: filePath.path!, encoding: NSUTF8StringEncoding, error: &error)!
dataString += data
dataString += "--\(boundaryConstant)--\r\n"
println(dataString) // This would allow you to see what the dataString looks like.
// Set the HTTPBody we'd like to submit
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData
// Make an asynchronous call so as not to hold up other processes.
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response, dataObject, error) in
if let HTTPResponse = response as? NSHTTPURLResponse {
let statusCode = HTTPResponse.statusCode
if statusCode == 200 {
NSNotificationCenter.defaultCenter().postNotificationName("IndoMagnetoNotificationKeySuccess", object: self)
} else {
NSNotificationCenter.defaultCenter().postNotificationName("IndoMagnetoNotificationKeyFailure", object: self)
}
}
if let apiError = error {
//aHandler?(obj: error, success: false)
NSNotificationCenter.defaultCenter().postNotificationName("IndoMagnetoNotificationKeyError", object: self)
}
})
}
}