2017-01-10 32 views
0

我正在使用swift 3.0 uisng backendless。我對這個概念很陌生。我正在上傳使用UIImagePickerController從手機庫中選擇的圖像。回到無盡的我正在使用Rest Api。我使用下面的代碼uplaoding圖像..如何在swift 3.0中使用Backendless上傳圖片

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」。

任何人都可以告訴我我在這裏做了什麼錯誤。 在此先感謝。

+0

你爲什麼不用他們的圖書館? https://backendless.com/documentation/files/ios/files_file_upload.htm –

+0

我只用於那個Api只有我可以得到400狀態代碼錯誤 –

+0

不,你試圖發送通過alamofire通過請求地址不存在,後端服務器如何知道您的文件本地路徑?使用他們做的上傳文件,你有部分爲 –

回答

0

從第一步開始,你完全錯了。 您所做的請求url是您成功上傳文件時後端服務將返回給您的返回URL。

關於向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 

更多詳細信息,可他們的文檔閱讀。

+0

我使用了「上傳功能」,但是如何查看我發送的圖像? –

相關問題