【问题标题】:How to upload image using Backendless in swift 3.0如何在 swift 3.0 中使用 Backendless 上传图片
【发布时间】:2017-01-10 07:23:24
【问题描述】:

我正在开发 swift 3.0 uisng 后端。我对这个概念很陌生。我正在上传使用 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”。

谁能告诉我我在这里犯了什么错误。 提前致谢。

【问题讨论】:

  • 我只使用了那个 Api,只有我得到 400 状态码错误
  • 不,您正试图通过请求不存在的地址通过 alamofire 发送,后端服务器如何知道您的文件本地路径?使用他们所做的上传文件,你有那个部分
  • 如果我想用app动态上传图片是什么意思?

标签: swift3 uiimagepickercontroller backendless


【解决方案1】:

从第一步开始,您就完全错了。 您发出的请求url是您上传文件成功时后端服务将返回给您的返回url。

关于后端服务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

更多细节,你可以阅读他们的文档。

【讨论】:

  • 我使用了“上传功能”,但是我如何才能看到我发送的图像?
猜你喜欢
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
相关资源
最近更新 更多