Reputation: 93
I am working on Swift 3.0 using Backendless. I am new to this concept. I am uploading an image which I am selecting from the phone gallery using UIImagePickerController
. In Backendless I am using Rest API. I am uploading the image using the following code:
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.uploadButton.isHidden = true
myImageView.contentMode = .scaleAspectFit
myImageView.image = image
let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageName!)
print(localPath!)
let imageurl = info[UIImagePickerControllerReferenceURL] as? NSURL
let imagename = imageurl?.lastPathComponent
print(imagename!)
//https://api.backendless.com/<application id>/<version name>/files/<path>/<file name>
Alamofire.request(“https://api.backendless.com/66B90F83-A813-84CF-FF9D-4A01AC28E100/v1/files/ + "\(localPath!)/\(imagename!)", method: .post, parameters: nil, encoding: JSONEncoding.default, headers: HeadersClass.allHeaders.headers).responseJSON { response in
debugPrint(response)
}
imagePicker.dismiss(animated: true, completion: nil)
}
But I am getting the “Status Code error = 400”.
Can anyone please tell me what mistake I did here?
Upvotes: 1
Views: 135
Reputation: 846
Your are totally wrong from first step. The request url you made is the return url that the backendless service will return back to you when you uploaded your file successfully.
regarding to backendless service, https://backendless.com/documentation/files/ios/files_file_upload.htm, you need to implement the functions that listed down here:
// Upload data block identified as 'content' to the specified path.
// If the server returns an error, it is delivered through
// the 'responder' object
func upload(_ path: String,
content content: NSData,
responder responder: IResponder!) -> Void
// Upload data block identified as 'content' to the specified path.
// If the file already exists on the server, overwrite if the
// 'overwrite' argument is set to YES/TRUE.
// If the server returns an error, it is delivered through
// the 'responder' object
func upload(_ path: String,
content content: NSData,
overwrite overwrite: Bool,
responder responder: IResponder!) -> Void
// Upload data block identified as 'content' to the specified path.
// If the server returns an error, it is delivered through
// the 'error' block-based callback
func upload(_ path: String,
content content: NSData,
response responseBlock: ((BackendlessFile!) -> Void)!,
error errorBlock: ((Fault!) -> Void)!) -> Void
// Upload data block identified as 'content' to the specified path.
// If the file already exists on the server, overwrite if the
// 'overwrite' argument is set to YES/TRUE.
// If the server returns an error, it is delivered through
// the 'error' block-based callback
func upload(_ path: String,
content content: NSData,
overwrite overwrite: Bool,
response responseBlock: ((BackendlessFile!) -> Void)!,
error errorBlock: ((Fault!) -> Void)!) -> Void
More detail, you can read in their document.
Upvotes: 0