Send parameters AND data to web server with Swift 3 - ios

I'm trying to figure out how to send a photo from an iPhone to my web server.
I also need to send parameters containing the size of the photo, it's filename and other additional information about the photo in the same request as the parameter data.
The code below is on the right track I think, but where do I put the parameter data called params:
let params: Array<String> = [aI.filename, String(aI.size), String(aI.dateTime.year), String(aI.dateTime.month), String(aI.dateTime.day), String(aI.dateTime.hour), String(aI.dateTime.minute), String(aI.dateTime.second), String(aI.dateTime.millisecond)]
var serverURL = URL(string: "http://192.168.0.23/upload.php");
var req = NSMutableURLRequest(url: serverURL!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 60.0);
//Set request to post
req.httpMethod = "POST";
//Set content type
req.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type");
let task = URLSession.sharedSession().dataTaskWithRequest(req){ data, response, error in
if error != nil{
print("Error -> \(error)")
return
}
do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
print("Result -> \(result)")
} catch {
print("Error -> \(error)")
}
}
task.resume()
return task

Allthough some of the answers pushed me in the right direction, they still didn't fit my project and so I continued googling an I managed to find exactly what I needed in the following article: http://swiftdeveloperblog.com/image-upload-example/
I needed to make the HTTP request asynchronously and using sessions,
which I didn't specify in the question because the question was merely about how to send both several parameters along with data in one single request.
It is called Multipart Form Data when doing so.
I had to modify the code from the article a little bit to make it work for my application,
so I'm sharing my Swift 3 code below:
Trigger code
let params = [
"filename" : chunkOwner.filename ,
"size" : String(describing: chunkOwner.size) ,
"year" : String(chunkOwner.dateTime.year) ,
"month" : String(chunkOwner.dateTime.month) ,
"day" : String(chunkOwner.dateTime.day) ,
"hour" : String(chunkOwner.dateTime.hour) ,
"minute" : String(chunkOwner.dateTime.minute) ,
"second" : String(chunkOwner.dateTime.second) ,
"millisecond" : String(chunkOwner.dateTime.millisecond) ,
]
uploadChunk(url: URL(string: "http://192.168.0.23/upload.php")!, data: photoData, params: params)
Upload code:
func uploadData(url: URL, data: Data!, params: [String: String])
{
let cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData;
let request = NSMutableURLRequest(url: url, cachePolicy: cachePolicy, timeoutInterval: 6.0);
request.httpMethod = "POST";
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if(data == nil) { return; }
request.httpBody = createBodyWithParameters(parameters: params, filePathKey: "file", data: data, boundary: boundary)
//myActivityIndicator.startAnimating();
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
// You can print out response object
print("******* response = \(response)")
// Print out reponse body
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("****** response data = \(responseString!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
print(json)
}catch
{
//if you recieve an error saying that the data could not be uploaded,
//make sure that the upload size is set to something higher than the size
print(error)
}
}
task.resume()
}
func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, data: Data!, boundary: String) -> Data {
var body = Data();
if parameters != nil {
for (key, value) in parameters! {
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
}
let mimetype = "text/csv"
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(parameters!["filename"]!)\"\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(data)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
Also include the following code at the bottom of your .swift file outside of your class:
extension Data {
mutating func appendString(string: String) {
append(string.data(using: .utf8)!)
}
}
And for the PHP upload script I did some changes and now looks like this:
<?php
$target_dir = "/var/www/html/uploads";if(!file_exists($target_dir)){
mkdir($target_dir, 0777, true);
}
$target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]);
echo count("size: ".$_FILES["file"]["tmp_name"]);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)){
echo json_encode([
"Message" => "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.",
"Status" => "OK",
]);
} else {
echo json_encode([
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error",
]);
}
?>
Important Note:
Your app will fail to upload data if your server php file called
php.ini is configured to accept files smaller than the data you're
trying to upload.
For example: If php.ini is configured to accept 2 MB, then any
uploads larger than 2 MB will be ignored and your app will receive a
response saying that something went wrong.
To change the file size acceptance in php.ini you need to look for
the variable called upload_max_filesize and post_max_sizeand change those to whatever file size
your system requires.

You can put them to httpBody or to httpBodyStream (by using NSInputStream)
But don't forget to transform params for server protocol (for example xml, json, or binary data with custom format).
For your content type (application/x-www-form-urlencoded), you can find format in wikipedia:
keyName=value&keyName2=value2
The keys and values should contain of URLPathAllowedCharacterSet, to achieve it you can use stringByAddingPercentEncodingWithAllowedCharacters.
To convert the KeyValue string to NSData, you can use method dataUsingEncoding.

I am sharing you one way of posting data using NSURLConnection in Swift3
Your URL
var serverURL = URL(string: "http://192.168.0.23/upload.php")
Your parameters to be like this , just discuss with server people to which parameters you to need pass data Then assign your value to that parameter like below
serverparameter1 = \(value to post)& serverparameter2 = \(value to post2).......
With your params I did like this have a look
let params = "filename= \(aI.filename)&size = \(String(aI.size))& dateTimeYear =\(String(aI.dateTime.year))&dateTimeMonth =\(String(aI.dateTime.month))& dateTimeDay =\(String(aI.dateTime.day))&dateTimeHour =\(String(aI.dateTime.hour))&dateTimeMinute =\(String(aI.dateTime.minute))&dateTimeSecond =\(String(aI.dateTime.second))&dateTimeMilliSecond=\(String(aI.dateTime.millisecond))"
Convert your Photo Data to Base64String like below
var base64String: NSString!
let myImage = UIImage(named:"image.png")
let imageData = UIImageJPEGRepresentation(myImage, 0.9)
base64String = imageData!.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithLineFeed) as NSString!
print(base64String)
then pass as stringParameter
&ImageDataStr = \(base64String)
then final Url seems to be look like
\(serverURL)/\(params)
OR
\(serverURL)/Upload?\(params)
Step by step request
var serverURL = URL(string: "http://192.168.0.23/upload.php")
let params = "filename= \(aI.filename)&size = \(String(aI.size))& dateTimeYear =\(String(aI.dateTime.year))&dateTimeMonth =\(String(aI.dateTime.month))& dateTimeDay =\(String(aI.dateTime.day))&dateTimeHour =\(String(aI.dateTime.hour))&dateTimeMinute =\(String(aI.dateTime.minute))&dateTimeSecond =\(String(aI.dateTime.second))&dateTimeMilliSecond=\(String(aI.dateTime.millisecond))&photoDataStr = \(base64String)"
var status:NSString = "\(serverURL)/Upload?\(params)" as NSString
status = status.addingPercentEscapes(using: String.Encoding.utf8.rawValue)! as NSString
let url = URL(string: status as String)!
let request = URLRequest(url: url, cachePolicy:NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 600)
// need synchronous
Here you will get responseData
var response:URLResponse?
var responseD:Data = try! NSURLConnection.sendSynchronousRequest(request, returning:&response)
Finally make that BinaryData to readable
// save to string - the result came from the Server call
var serverResults:NSString = NSString(data: responseD, encoding: String.Encoding.utf8.rawValue)!
print(serverResults)
For Example your Result
if serverResults.range(of: "RESULT>APPROVED").location != NSNotFound
{
return "Data posted"
}
else
{
return "Failed to post"
}

Related

Swift 5, multipart/form-data with multiple body parameters [duplicate]

I'm trying to upload an image with parameters in Swift. When I try this code, I can get the parameters but not the image
uploadFileToUrl(fotiño:UIImage){
var foto = UIImage(data: UIImageJPEGRepresentation(fotiño, 0.2))
var request = NSMutableURLRequest(URL:NSURL(string: "URL"))
request.HTTPMethod = "POST"
var bodyData = "id_user="PARAMETERS&ETC""
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
request.HTTPBody = NSData.dataWithData(UIImagePNGRepresentation(foto))
println("miraqui \(request.debugDescription)")
var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
var HTTPError: NSError? = nil
var JSONError: NSError? = nil
var dataVal: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: &HTTPError)
if ((dataVal != nil) && (HTTPError == nil)) {
var jsonResult = NSJSONSerialization.JSONObjectWithData(dataVal!, options: NSJSONReadingOptions.MutableContainers, error: &JSONError)
if (JSONError != nil) {
println("Bad JSON")
} else {
println("Synchronous\(jsonResult)")
}
} else if (HTTPError != nil) {
println("Request failed")
} else {
println("No Data returned")
}
}
edit 2:
I think that I have some problems with the path of the saved UIImage, because php tells me that the file already exist, which I think is because I send it in blank
func createRequest (#userid: String, disco: String, id_disco: String, pub: String, foto: UIImage) -> NSURLRequest {
let param = [
"id_user" : userid,
"name_discoteca" : disco,
"id_discoteca" : id_disco,
"ispublic" : pub] // build your dictionary however appropriate
let boundary = generateBoundaryString()
let url = NSURL(string: "http....")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.timeoutInterval = 60
request.HTTPShouldHandleCookies = false
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var imagesaver = ImageSaver()
var image = foto // However you create/get a UIImage
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent("VipKing.jpg")
UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath, atomically: true)
self.saveImage(foto, withFileName: "asdasd22.jpg")
var path = self.documentsPathForFileName("asdasd22.jpg")
self.ViewImage.image = self.loadImageWithFileName("asdasd22.jpg")
// let path1 = NSBundle.mainBundle().pathForResource("asdasd22", ofType: "jpg", inDirectory: path) as String!
**//path1 always crash**
println(param.debugDescription)
println(path.debugDescription)
println(boundary.debugDescription)
request.HTTPBody = createBodyWithParameters(param, filePathKey: "asdasd22.jpg", paths: [path], boundary: boundary)
println(request.debugDescription)
return request
}
In your comment below, you inform us that you are using the $_FILES syntax to retrieve the files. That means that you want to create a multipart/form-data request. The process is basically:
Specify a boundary for your multipart/form-data request.
Specify a Content-Type of the request that specifies that it multipart/form-data and what the boundary is.
Create body of request, separating the individual components (each of the posted values as well as between each upload).
For more detail, see RFC 7578. Anyway, in Swift 3 and later, this might look like:
/// Create request
///
/// - parameter userid: The userid to be passed to web service
/// - parameter password: The password to be passed to web service
/// - parameter email: The email address to be passed to web service
///
/// - returns: The `URLRequest` that was created
func createRequest(userid: String, password: String, email: String) throws -> URLRequest {
let parameters = [
"user_id" : userid,
"email" : email,
"password" : password] // build your dictionary however appropriate
let boundary = generateBoundaryString()
let url = URL(string: "https://example.com/imageupload.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let fileURL = Bundle.main.url(forResource: "image1", withExtension: "png")!
request.httpBody = try createBody(with: parameters, filePathKey: "file", urls: [fileURL], boundary: boundary)
return request
}
/// Create body of the `multipart/form-data` request
///
/// - parameter parameters: The optional dictionary containing keys and values to be passed to web service.
/// - parameter filePathKey: The optional field name to be used when uploading files. If you supply paths, you must supply filePathKey, too.
/// - parameter urls: The optional array of file URLs of the files to be uploaded.
/// - parameter boundary: The `multipart/form-data` boundary.
///
/// - returns: The `Data` of the body of the request.
private func createBody(with parameters: [String: String]? = nil, filePathKey: String, urls: [URL], boundary: String) throws -> Data {
var body = Data()
parameters?.forEach { (key, value) in
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.append("\(value)\r\n")
}
for url in urls {
let filename = url.lastPathComponent
let data = try Data(contentsOf: url)
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(filePathKey)\"; filename=\"\(filename)\"\r\n")
body.append("Content-Type: \(url.mimeType)\r\n\r\n")
body.append(data)
body.append("\r\n")
}
body.append("--\(boundary)--\r\n")
return body
}
/// Create boundary string for multipart/form-data request
///
/// - returns: The boundary string that consists of "Boundary-" followed by a UUID string.
private func generateBoundaryString() -> String {
return "Boundary-\(UUID().uuidString)"
}
With:
extension URL {
/// Mime type for the URL
///
/// Requires `import UniformTypeIdentifiers` for iOS 14 solution.
/// Requires `import MobileCoreServices` for pre-iOS 14 solution
var mimeType: String {
if #available(iOS 14.0, *) {
return UTType(filenameExtension: pathExtension)?.preferredMIMEType ?? "application/octet-stream"
} else {
guard
let identifier = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, nil)?.takeRetainedValue(),
let mimeType = UTTypeCopyPreferredTagWithClass(identifier, kUTTagClassMIMEType)?.takeRetainedValue() as String?
else {
return "application/octet-stream"
}
return mimeType
}
}
}
extension Data {
/// Append string to Data
///
/// Rather than littering my code with calls to `data(using: .utf8)` to convert `String` values to `Data`, this wraps it in a nice convenient little extension to Data. This defaults to converting using UTF-8.
///
/// - parameter string: The string to be added to the `Data`.
mutating func append(_ string: String, using encoding: String.Encoding = .utf8) {
if let data = string.data(using: encoding) {
append(data)
}
}
}
Having all of this, you now need to submit this request. I would advise this is done asynchronously. For example, using URLSession, you would do something like:
let request: URLRequest
do {
request = try createRequest(userid: userid, password: password, email: email)
} catch {
print(error)
return
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
// handle error here
print(error ?? "Unknown error")
return
}
// parse `data` here, then parse it
// note, if you want to update the UI, make sure to dispatch that to the main queue, e.g.:
//
// DispatchQueue.main.async {
// // update your UI and model objects here
// }
}
task.resume()
If you are uploading large assets (e.g. videos or the like), you might want to use a file-based permutation of the above. See https://stackoverflow.com/a/70552269/1271826.
For Swift 2 renditions, see previous revision of this answer.
AlamoFire now supports Multipart:
https://github.com/Alamofire/Alamofire#uploading-multipartformdata
Here's a blog post with sample project that touches on using Multipart with AlamoFire.
http://www.thorntech.com/2015/07/4-essential-swift-networking-tools-for-working-with-rest-apis/
The relevant code might look something like this (assuming you're using AlamoFire and SwiftyJSON):
func createMultipart(image: UIImage, callback: Bool -> Void){
// use SwiftyJSON to convert a dictionary to JSON
var parameterJSON = JSON([
"id_user": "test"
])
// JSON stringify
let parameterString = parameterJSON.rawString(encoding: NSUTF8StringEncoding, options: nil)
let jsonParameterData = parameterString!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
// convert image to binary
let imageData = UIImageJPEGRepresentation(image, 0.7)
// upload is part of AlamoFire
upload(
.POST,
URLString: "http://httpbin.org/post",
multipartFormData: { multipartFormData in
// fileData: puts it in "files"
multipartFormData.appendBodyPart(fileData: jsonParameterData!, name: "goesIntoFile", fileName: "json.txt", mimeType: "application/json")
multipartFormData.appendBodyPart(fileData: imageData, name: "file", fileName: "iosFile.jpg", mimeType: "image/jpg")
// data: puts it in "form"
multipartFormData.appendBodyPart(data: jsonParameterData!, name: "goesIntoForm")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { request, response, data, error in
let json = JSON(data!)
println("json:: \(json)")
callback(true)
}
case .Failure(let encodingError):
callback(false)
}
}
)
}
let fotoImage = UIImage(named: "foto")
createMultipart(fotoImage!, callback: { success in
if success { }
})
Thank you #Rob, your code is working fine, but in my case, I am retriving image from gallary and taking name of the image by using code:
let filename = url.lastPathComponent
But this code, displaying image extension as .JPG (in capital letter), but server not accepting extensions in captital letter, so i changed my code as:
let filename = (path.lastPathComponent as NSString).lowercaseString
and now my code is working fine.
Thank you :)

How to send a image file to server using in post function using swift 4 [duplicate]

I'm trying to upload an image with parameters in Swift. When I try this code, I can get the parameters but not the image
uploadFileToUrl(fotiño:UIImage){
var foto = UIImage(data: UIImageJPEGRepresentation(fotiño, 0.2))
var request = NSMutableURLRequest(URL:NSURL(string: "URL"))
request.HTTPMethod = "POST"
var bodyData = "id_user="PARAMETERS&ETC""
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
request.HTTPBody = NSData.dataWithData(UIImagePNGRepresentation(foto))
println("miraqui \(request.debugDescription)")
var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
var HTTPError: NSError? = nil
var JSONError: NSError? = nil
var dataVal: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: &HTTPError)
if ((dataVal != nil) && (HTTPError == nil)) {
var jsonResult = NSJSONSerialization.JSONObjectWithData(dataVal!, options: NSJSONReadingOptions.MutableContainers, error: &JSONError)
if (JSONError != nil) {
println("Bad JSON")
} else {
println("Synchronous\(jsonResult)")
}
} else if (HTTPError != nil) {
println("Request failed")
} else {
println("No Data returned")
}
}
edit 2:
I think that I have some problems with the path of the saved UIImage, because php tells me that the file already exist, which I think is because I send it in blank
func createRequest (#userid: String, disco: String, id_disco: String, pub: String, foto: UIImage) -> NSURLRequest {
let param = [
"id_user" : userid,
"name_discoteca" : disco,
"id_discoteca" : id_disco,
"ispublic" : pub] // build your dictionary however appropriate
let boundary = generateBoundaryString()
let url = NSURL(string: "http....")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.timeoutInterval = 60
request.HTTPShouldHandleCookies = false
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var imagesaver = ImageSaver()
var image = foto // However you create/get a UIImage
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent("VipKing.jpg")
UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath, atomically: true)
self.saveImage(foto, withFileName: "asdasd22.jpg")
var path = self.documentsPathForFileName("asdasd22.jpg")
self.ViewImage.image = self.loadImageWithFileName("asdasd22.jpg")
// let path1 = NSBundle.mainBundle().pathForResource("asdasd22", ofType: "jpg", inDirectory: path) as String!
**//path1 always crash**
println(param.debugDescription)
println(path.debugDescription)
println(boundary.debugDescription)
request.HTTPBody = createBodyWithParameters(param, filePathKey: "asdasd22.jpg", paths: [path], boundary: boundary)
println(request.debugDescription)
return request
}
In your comment below, you inform us that you are using the $_FILES syntax to retrieve the files. That means that you want to create a multipart/form-data request. The process is basically:
Specify a boundary for your multipart/form-data request.
Specify a Content-Type of the request that specifies that it multipart/form-data and what the boundary is.
Create body of request, separating the individual components (each of the posted values as well as between each upload).
For more detail, see RFC 7578. Anyway, in Swift 3 and later, this might look like:
/// Create request
///
/// - parameter userid: The userid to be passed to web service
/// - parameter password: The password to be passed to web service
/// - parameter email: The email address to be passed to web service
///
/// - returns: The `URLRequest` that was created
func createRequest(userid: String, password: String, email: String) throws -> URLRequest {
let parameters = [
"user_id" : userid,
"email" : email,
"password" : password] // build your dictionary however appropriate
let boundary = generateBoundaryString()
let url = URL(string: "https://example.com/imageupload.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let fileURL = Bundle.main.url(forResource: "image1", withExtension: "png")!
request.httpBody = try createBody(with: parameters, filePathKey: "file", urls: [fileURL], boundary: boundary)
return request
}
/// Create body of the `multipart/form-data` request
///
/// - parameter parameters: The optional dictionary containing keys and values to be passed to web service.
/// - parameter filePathKey: The optional field name to be used when uploading files. If you supply paths, you must supply filePathKey, too.
/// - parameter urls: The optional array of file URLs of the files to be uploaded.
/// - parameter boundary: The `multipart/form-data` boundary.
///
/// - returns: The `Data` of the body of the request.
private func createBody(with parameters: [String: String]? = nil, filePathKey: String, urls: [URL], boundary: String) throws -> Data {
var body = Data()
parameters?.forEach { (key, value) in
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.append("\(value)\r\n")
}
for url in urls {
let filename = url.lastPathComponent
let data = try Data(contentsOf: url)
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(filePathKey)\"; filename=\"\(filename)\"\r\n")
body.append("Content-Type: \(url.mimeType)\r\n\r\n")
body.append(data)
body.append("\r\n")
}
body.append("--\(boundary)--\r\n")
return body
}
/// Create boundary string for multipart/form-data request
///
/// - returns: The boundary string that consists of "Boundary-" followed by a UUID string.
private func generateBoundaryString() -> String {
return "Boundary-\(UUID().uuidString)"
}
With:
extension URL {
/// Mime type for the URL
///
/// Requires `import UniformTypeIdentifiers` for iOS 14 solution.
/// Requires `import MobileCoreServices` for pre-iOS 14 solution
var mimeType: String {
if #available(iOS 14.0, *) {
return UTType(filenameExtension: pathExtension)?.preferredMIMEType ?? "application/octet-stream"
} else {
guard
let identifier = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, nil)?.takeRetainedValue(),
let mimeType = UTTypeCopyPreferredTagWithClass(identifier, kUTTagClassMIMEType)?.takeRetainedValue() as String?
else {
return "application/octet-stream"
}
return mimeType
}
}
}
extension Data {
/// Append string to Data
///
/// Rather than littering my code with calls to `data(using: .utf8)` to convert `String` values to `Data`, this wraps it in a nice convenient little extension to Data. This defaults to converting using UTF-8.
///
/// - parameter string: The string to be added to the `Data`.
mutating func append(_ string: String, using encoding: String.Encoding = .utf8) {
if let data = string.data(using: encoding) {
append(data)
}
}
}
Having all of this, you now need to submit this request. I would advise this is done asynchronously. For example, using URLSession, you would do something like:
let request: URLRequest
do {
request = try createRequest(userid: userid, password: password, email: email)
} catch {
print(error)
return
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
// handle error here
print(error ?? "Unknown error")
return
}
// parse `data` here, then parse it
// note, if you want to update the UI, make sure to dispatch that to the main queue, e.g.:
//
// DispatchQueue.main.async {
// // update your UI and model objects here
// }
}
task.resume()
If you are uploading large assets (e.g. videos or the like), you might want to use a file-based permutation of the above. See https://stackoverflow.com/a/70552269/1271826.
For Swift 2 renditions, see previous revision of this answer.
AlamoFire now supports Multipart:
https://github.com/Alamofire/Alamofire#uploading-multipartformdata
Here's a blog post with sample project that touches on using Multipart with AlamoFire.
http://www.thorntech.com/2015/07/4-essential-swift-networking-tools-for-working-with-rest-apis/
The relevant code might look something like this (assuming you're using AlamoFire and SwiftyJSON):
func createMultipart(image: UIImage, callback: Bool -> Void){
// use SwiftyJSON to convert a dictionary to JSON
var parameterJSON = JSON([
"id_user": "test"
])
// JSON stringify
let parameterString = parameterJSON.rawString(encoding: NSUTF8StringEncoding, options: nil)
let jsonParameterData = parameterString!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
// convert image to binary
let imageData = UIImageJPEGRepresentation(image, 0.7)
// upload is part of AlamoFire
upload(
.POST,
URLString: "http://httpbin.org/post",
multipartFormData: { multipartFormData in
// fileData: puts it in "files"
multipartFormData.appendBodyPart(fileData: jsonParameterData!, name: "goesIntoFile", fileName: "json.txt", mimeType: "application/json")
multipartFormData.appendBodyPart(fileData: imageData, name: "file", fileName: "iosFile.jpg", mimeType: "image/jpg")
// data: puts it in "form"
multipartFormData.appendBodyPart(data: jsonParameterData!, name: "goesIntoForm")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { request, response, data, error in
let json = JSON(data!)
println("json:: \(json)")
callback(true)
}
case .Failure(let encodingError):
callback(false)
}
}
)
}
let fotoImage = UIImage(named: "foto")
createMultipart(fotoImage!, callback: { success in
if success { }
})
Thank you #Rob, your code is working fine, but in my case, I am retriving image from gallary and taking name of the image by using code:
let filename = url.lastPathComponent
But this code, displaying image extension as .JPG (in capital letter), but server not accepting extensions in captital letter, so i changed my code as:
let filename = (path.lastPathComponent as NSString).lowercaseString
and now my code is working fine.
Thank you :)

image uploading to laravel server iOS

In my app i want to upload few images to server. The issue is that i am not able to upload it. The same code works fine for servers and i am able to upload the image.But for laravel server i am not able to upload it.
Code
func sendImageWithParams(image:UIImage, url : String,photoParamKey:String,params : [String:String],quality:CGFloat=0.7,getResponse : Response) -> Void {
let request = NSMutableURLRequest(URL: NSURL(string: url)!);
request.HTTPMethod = "POST"
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData = UIImageJPEGRepresentation(image, quality)
if(imageData==nil) { return; }
request.HTTPBody = createBodyWithParameters(params,photoParamKey:photoParamKey, imageDataKey: imageData!, boundary: boundary)
request.timeoutInterval = 20.0
let task = NSURLSession.sharedSession().dataTaskWithRequest(request,
completionHandler: {
(data, response, error) -> Void in
if let data = data {
getResponse(success: data, error: nil)
} else if let error = error {
getResponse(success: nil , error: error)
}
})
task.resume()
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().UUIDString)"
}
func createBodyWithParameters(parameters: [String: String]?,photoParamKey : String,imageDataKey: NSData, boundary: String) -> NSData {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
let mimetype = "image/jpg"
let filename = "image"
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(photoParamKey)\"; filename=\"\(filename)\"\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.appendData(imageDataKey)
body.appendString("\r\n")
body.appendString("--\(boundary)--\r\n")
return body
}
}
Response which i am getting
<OS_dispatch_data: data[0x7fe220759040] = { leaf, size = 30, buf = 0x7fe22063cff0 }>
Optional({"status_code":422,"status":"Failure","response":{"userId":["The user id field is required."],"propertyId":["The property id field is required."],"title":["The title field is required."],"description":["The description field is required."]},"message":"422 Unprocessable Entity","debug":{"line":22,"file":"\/var\/www\/html\/embassy\/vendor\/dingo\/api\/src\/Http\/FormRequest.php","class":"Dingo\\Api\\Exception\\ValidationHttpException","trace":["#0 \/var\/www\/html\/embassy\/vendor\/laravel\/framework\/src\/Illuminate\/Validation\/ValidatesWhenResolvedTrait.php(25): Dingo\\Api\\Http\\FormRequest->failedValidation
The way i am using the function
let p = ["title" : "a",
"propertyId":"1",
"description":"hey",
"userId":"131",
"params":"mobile"
]
NetworkRequest.sharedInstance.sendImageWithParams(imageView.image!, url: "http://52.66.131.92/api/blogs", photoParamKey: "photo", params: p, getResponse: { (success, error) in
print("Error:\(error)")
print("Success:\(success)")
})
P.S -> I am sending all the parameters correctly!.
Either you are hitting the wrong API endpoint or passing incorrect params, Since, in your code, you are trying to upload an image, but the API endpoint needs the userId, propertyId, title and description which are necessary parameters to accept the image. I am unable to see these values in your request.

Upload image to server - Swift 3

I want to send an image as a parameter along with my request.
I have used the code below to call my POST request but I don't know how to append image to the body.
I am getting the image through image picker as follows:
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
My request is formed as below
var request = URLRequest(url: URL(string: "")!) // link removed
request.httpMethod = "POST"
let postString = "user_id=\(userId)&image=\(image)"
request.httpBody = postString.data(using:.utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
return
}
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? AnyObject
if let parseJSON = json {
print("resp :\(parseJSON)")
}
} catch let error as NSError {
print("error : \(error)")
}
}
task.resume()
I am new to Swift. I have seen this through multipart/form-data but unable to implement it myself. I do not want to encode it in base 64 format. Please help me in this.
I use the following structure for sending images:
func createRequestBodyWith(parameters:[String:NSObject], filePathKey:String, boundary:String) -> NSData {
let body = NSMutableData()
for (key, value) in parameters {
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
body.appendString(string: "--\(boundary)\r\n")
let mimetype = "image/jpg"
let defFileName = "yourImageName.jpg"
let imageData = UIImageJPEGRepresentation(yourImage, 1)
body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(defFileName)\"\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(imageData!)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
extension NSMutableData {
func appendString(string: String) {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)
}
}
then you have to create body in your function like following:
request.httpBody = self.createRequestBodyWith(parameters:yourParamsDictionary, filePathKey:yourKey, boundary:self.generateBoundaryString)
Swift 4.2
func uploadImage(paramName: String, fileName: String, image: UIImage) {
let url = URL(string: "http://api-host-name/v1/api/uploadfile/single")
// generate boundary string using a unique per-app string
let boundary = UUID().uuidString
let session = URLSession.shared
// Set the URLRequest to POST and to the specified URL
var urlRequest = URLRequest(url: url!)
urlRequest.httpMethod = "POST"
// Set Content-Type Header to multipart/form-data, this is equivalent to submitting form data with file upload in a web browser
// And the boundary is also set here
urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var data = Data()
// Add the image data to the raw http request data
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
data.append("Content-Disposition: form-data; name=\"\(paramName)\"; filename=\"\(fileName)\"\r\n".data(using: .utf8)!)
data.append("Content-Type: image/png\r\n\r\n".data(using: .utf8)!)
data.append(image.pngData()!)
data.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)
// Send a POST request to the URL, with the data we created earlier
session.uploadTask(with: urlRequest, from: data, completionHandler: { responseData, response, error in
if error == nil {
let jsonData = try? JSONSerialization.jsonObject(with: responseData!, options: .allowFragments)
if let json = jsonData as? [String: Any] {
print(json)
}
}
}).resume()
}
If you have any header to add, you can add it via urlRequest.setValue method.
Source: https://fluffy.es/upload-image-to-server/
Original Thread:
upload image to server using Alamofire
answer by Ekta Padaliya
Upload Image Using AlamoFire
Installation guide please follow tutorials
https://github.com/Alamofire/Alamofire
Language used Swift 4
Import AlamoFire Library
import Alamofire
Create a custom Method. (can paste this in UIViewController Extension / your UIViewcontroller as new function)
func uploadImage(img: UIImage){
let ImageData = UIImagePNGRepresentation(img)
let urlReq = "http://apiUrl.php"
let parameters = ["user_id": "useridValue"]//you can comment this if not needed
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(ImageData!, withName: "shop_logo",fileName: "file.jpg", mimeType: "image/jpg")
for (key, value) in parameters {// this will loop the 'parameters' value, you can comment this if not needed
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to:urlReq)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
if let dic = response.result.value as? NSDictionary{
//do your action base on Api Return failed/success
}
}
case .failure(let encodingError):
print(encodingError)
}
}
}
Calling the method like so
self.uploadImage(img: image)

Sending video on server by multipart form data

I have a problem with pushing video on the server. My API has two parameters in body: details and file and I must authorize it by token in header.
At the beginning my file was prepared and exported to the URL that I am pushing to method. Next with details (string value) I am trying to prepare it to send on the server.
After I run my app I have got this error:
Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}
Server in respond returns all data about pushed video in JSON
I used this tutorial to try make some first step:
https://mindfiremobile.wordpress.com/2014/01/27/upload-video-to-server-using-multiparts/
It my first steps with pushing video on the server and I will be glad for help.
func postDetailsWithVideo(details: String, file: NSURL) {
let url = serverURL.URLByAppendingPathComponent("api/details")
let videoData = NSData.init(contentsOfURL: file)
let kBoundary = "---------------------------14737809831466499882746641449"
let kStartTag = "--%#\r\n"
let kEndTag = "\r\n"
let kContent = "Content-Disposition: form-data; name=\"%#\"\r\n\r\n"
let body = NSMutableData()
//details data
body.appendData(String(format: kStartTag, kBoundary).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(String(format: kContent, "storyDetails").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(details.dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(String(format: kEndTag).dataUsingEncoding(NSUTF8StringEncoding)!)
//Video data
body.appendData(String(format: kStartTag, kBoundary).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition: form-data; file=\"flv\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: application/octet-stream\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSData(data: videoData!))
body.appendData(String(format: kEndTag).dataUsingEncoding(NSUTF8StringEncoding)!)
// close form
body.appendData("--\(kBoundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
let contentType = "multipart/form-data; boundary=\(kBoundary)"
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("Bearer \(DataManager().getAccessToken())", forHTTPHeaderField: "Authorization")
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.HTTPBody = body
print(body)
postDataOnTheServer(request, apiMethod: "api/details")
}
func postDataOnTheServer(request: NSMutableURLRequest, apiMethod: NSString) {
let currentRequest = request
let task = NSURLSession.sharedSession().dataTaskWithRequest(currentRequest){ data, response, error in
if error != nil{
self.delegate?.errorOccured(apiMethod, error: error!)
return
}
do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
self.delegate?.serverResponseFromAPIMethod(apiMethod, result: result!)
} catch {
print("response - some error")
print(error) //do something with me
}
}
task.resume()
}
Its a server issue.Ask your backend not to echo anything and you will get the response.

Resources