{"id":7846,"date":"2015-10-31T12:28:10","date_gmt":"2015-10-31T12:28:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/31\/uploading-file-with-parameters-using-alamofire-open-source-projects-alamofire-alamofire\/"},"modified":"2015-10-31T12:28:10","modified_gmt":"2015-10-31T12:28:10","slug":"uploading-file-with-parameters-using-alamofire-open-source-projects-alamofire-alamofire","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/31\/uploading-file-with-parameters-using-alamofire-open-source-projects-alamofire-alamofire\/","title":{"rendered":"Uploading file with parameters using Alamofire-open source projects Alamofire\/Alamofire"},"content":{"rendered":"<p>I took antiblank&#8217;s answer and wrapped this all up in 1 function with completion handler. Thought it might be useful for someone. It&#8217;s a bit &#8216;rougher&#8217; then antiblank&#8217;s answer as I&#8217;m simply taking a string response from the PHP file (not JSON).<\/p>\n<p>Here&#8217;s how you call it:<\/p>\n<pre><code>let imageData = UIImagePNGRepresentation(myImageView.image)\n\nuploadImage(\"http:\/\/www.example.com\/image_upload.php\", imageData: imageData, subdir: \"images\", filename: \"imageID.png\")\n    { (req, res, str, err) -&gt; Void in\n        \/\/ do whatever you want to to for error handling and handeling success\n    }\n<\/code><\/pre>\n<p>Here&#8217;s the function itself:<\/p>\n<pre><code>func uploadImage(urlToPHPFile: String, imageData: NSData, subdir: String, filename: String, completionHandler:(request:NSURLRequest, response:NSURLResponse?, responseString:String?, error: NSError?) -&gt; ()) {\n\n    func urlRequestWithComponents(urlString:String, parameters:Dictionary, imageData:NSData) -&gt; (URLRequestConvertible, NSData) {\n        \/\/ create url request to send\n        var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)\n        mutableURLRequest.HTTPMethod = Method.POST.rawValue\n        let boundaryConstant = \"myRandomBoundary12345\";\n        let contentType = \"multipart\/form-data;boundary=\"+boundaryConstant\n        mutableURLRequest.setValue(contentType, forHTTPHeaderField: \"Content-Type\")\n\n        \/\/ create upload data to send\n        let uploadData = NSMutableData()\n\n        \/\/ add image\n        uploadData.appendData(\"\\r\\n--\\(boundaryConstant)\\r\\n\".dataUsingEncoding(NSUTF8StringEncoding)!)\n        uploadData.appendData(\"Content-Disposition: form-data; name=\\\"file\\\"; filename=\\\"file.png\\\"\\r\\n\".dataUsingEncoding(NSUTF8StringEncoding)!)\n        uploadData.appendData(\"Content-Type: image\/png\\r\\n\\r\\n\".dataUsingEncoding(NSUTF8StringEncoding)!)\n        uploadData.appendData(imageData)\n\n        \/\/ add parameters\n        for (key, value) in parameters {\n            uploadData.appendData(\"\\r\\n--\\(boundaryConstant)\\r\\n\".dataUsingEncoding(NSUTF8StringEncoding)!)\n            uploadData.appendData(\"Content-Disposition: form-data; name=\\\"\\(key)\\\"\\r\\n\\r\\n\\(value)\".dataUsingEncoding(NSUTF8StringEncoding)!)\n        }\n        uploadData.appendData(\"\\r\\n--\\(boundaryConstant)--\\r\\n\".dataUsingEncoding(NSUTF8StringEncoding)!)\n\n        \/\/ return URLRequestConvertible and NSData\n        return (ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)\n    }\n\n    let parameters = [\n        \"subdir\" : subdir,\n        \"filename\": filename\n    ]\n    let urlRequest = urlRequestWithComponents(urlToPHPFile, parameters, imageData)\n\n    AlamoFire.upload(urlRequest.0, urlRequest.1)\n        .responseString(completionHandler: { [weak self] (req, res, str, err) -&gt; Void in\n            if let strongSelf = self {\n                completionHandler(request: req, response: res, responseString: str, error: err)\n\n            }\n        }\n    )\n}\n<\/code><\/pre>\n<p>And here is the php file.<\/p>\n<pre><code>$subdir = $_POST['subdir'];\n$filename = $_POST[\"filename\"];\n\n$targetPath = $subdir.'\/'.$filename;\n\n$moved = move_uploaded_file($_FILES[\"file\"][\"tmp_name\"], $targetPath );\nif ($moved) {\n    echo \"OK\";\n}\nelse {\n    echo \"Error: file not uploaded\";\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I took antiblank&#8217;s answer and wrapped this all up in 1 function with completion handler. Thought it might be useful for someone. It&#8217;s a bit &#8216;rougher&#8217; then antiblank&#8217;s answer as I&#8217;m simply taking a string response from the PHP file (not JSON). Here&#8217;s how you call it: let imageData = UIImagePNGRepresentation(myImageView.image) uploadImage(&#8220;http:\/\/www.example.com\/image_upload.php&#8221;, imageData: imageData, subdir: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7846","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7846"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7846\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}