我正在使用无后端开发 Swift 3.0。我对这个概念很陌生。我正在上传使用 UIImagePickerController 从手机图库中选择的图像。在无尽的后面我正在使用 Rest Api。我正在使用以下代码上传图像..
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)
}
但我收到“状态代码错误 = 400”。
任何人都可以告诉我我在这里犯了什么错误吗? 提前致谢。
你从第一步就完全错了。 您发出的请求网址是当您成功上传文件时后端服务将返回给您的返回网址。
关于backendless服务,https://backendless.com/documentation/files/ios/files_file_upload.htm,您需要实现下面列出的功能:
// 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
更详细的信息,您可以阅读他们的文档。