-
-
Save morizotter/c1dd960df6878010d7ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FileUploader.swift | |
// ERAssistant | |
// | |
// Created by Narciso Cerezo Jiménez on 27/5/15. | |
// Copyright (c) 2015 Closure Software. All rights reserved. | |
// | |
import Foundation | |
import Alamofire | |
private struct FileUploadInfo { | |
var name:String | |
var mimeType:String | |
var fileName:String | |
var url:NSURL? | |
var data:NSData? | |
init( name: String, withFileURL url: NSURL, withMimeType mimeType: String? = nil ) { | |
self.name = name | |
self.url = url | |
self.fileName = name | |
self.mimeType = "application/octet-stream" | |
if mimeType != nil { | |
self.mimeType = mimeType! | |
} | |
if let _name = url.lastPathComponent { | |
fileName = _name | |
} | |
if mimeType == nil, let _extension = url.pathExtension { | |
switch _extension.lowercaseString { | |
case "jpeg", "jpg": | |
self.mimeType = "image/jpeg" | |
case "png": | |
self.mimeType = "image/png" | |
default: | |
self.mimeType = "application/octet-stream" | |
} | |
} | |
} | |
init( name: String, withData data: NSData, withMimeType mimeType: String ) { | |
self.name = name | |
self.data = data | |
self.fileName = name | |
self.mimeType = mimeType | |
} | |
} | |
class FileUploader { | |
private var parameters = [String:String]() | |
private var files = [FileUploadInfo]() | |
private var headers = [String:String]() | |
func setValue( value: String, forParameter parameter: String ) { | |
parameters[parameter] = value | |
} | |
func setValue( value: String, forHeader header: String ) { | |
headers[header] = value | |
} | |
func addParametersFrom( #map: [String:String] ) { | |
for (key,value) in map { | |
parameters[key] = value | |
} | |
} | |
func addHeadersFrom( #map: [String:String] ) { | |
for (key,value) in map { | |
headers[key] = value | |
} | |
} | |
func addFileURL( url: NSURL, withName name: String, withMimeType mimeType:String? = nil ) { | |
files.append( FileUploadInfo( name: name, withFileURL: url, withMimeType: mimeType ) ) | |
} | |
func addFileData( data: NSData, withName name: String, withMimeType mimeType:String = "application/octet-stream" ) { | |
files.append( FileUploadInfo( name: name, withData: data, withMimeType: mimeType ) ) | |
} | |
func uploadFile( request sourceRequest: NSURLRequest ) -> Request? { | |
var request = sourceRequest.mutableCopy() as! NSMutableURLRequest | |
let boundary = "FileUploader-boundary-\(arc4random())-\(arc4random())" | |
request.setValue( "multipart/form-data;boundary=\(boundary)", forHTTPHeaderField: "Content-Type") | |
let data = NSMutableData() | |
for (name, value) in headers { | |
request.setValue(value, forHTTPHeaderField: name) | |
} | |
for fileUploadInfo in files { | |
data.appendData( "\r\n--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)! ) | |
data.appendData( "Content-Disposition: form-data; name=\"\(fileUploadInfo.name)\"; filename=\"\(fileUploadInfo.fileName)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) | |
data.appendData( "Content-Type: \(fileUploadInfo.mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) | |
if fileUploadInfo.data != nil { | |
data.appendData( fileUploadInfo.data! ) | |
} | |
else if fileUploadInfo.url != nil, let fileData = NSData(contentsOfURL: fileUploadInfo.url!) { | |
data.appendData( fileData ) | |
} | |
else { // ToDo: report error | |
return nil | |
} | |
} | |
for (key, value) in parameters { | |
data.appendData("\r\n--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) | |
data.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!) | |
} | |
data.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) | |
return Alamofire.upload( request, data ) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example uploads a file called example.png found in the app resources | |
let fileURL = NSBundle.mainBundle().URLForResource("example", withExtension: "png") | |
let fileUploader = FileUploader() | |
// we can add multiple files | |
// this would be equivalent to: <input type="file" name="myFile"/> | |
fileUploader.addFileURL(fileURL!, withName: "myFile") | |
// we can add NSData objects directly | |
let data = UIImage(named: "sample") | |
fileUploader.addFileData( UIImageJPEGRepresentation(data,0.8), withName: "mySecondFile", withMimeType: "image/jpeg" ) | |
// we can also add multiple aditional parameters | |
// this would be equivalent to: <input type="hidden" name="folderName" value="sample"/> | |
fileUploader.setValue( "sample", forParameter: "folderName" ) | |
// put your server URL here | |
var request = NSMutableURLRequest( URL: NSURL(string: "http://myserver.com/uploadFile" )! ) | |
request.HTTPMethod = "POST" | |
fileUploader.uploadFile(request: request) |
can you sent me php file webservice. thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for Your work !
I can not upload files to 2 MB superiors when my server is parametrer to 6MB. Did you have a solution?