0

I want to send file of image on server when I edit choose image button, but I don't know why my image profile didn't update on sever , here is my console debugger area when app run :

1

and also I have this message from server that "please upload an image" this response should change to "your profile photo updated" and also this my code :

 func imageInput(asset : String) {
    let semaphore = DispatchSemaphore (value: 0)
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/" + asset
    let parameters = [
      [
        "key": "image",
        "src": "\(path)",
        "type": "file"
      ]] as [[String : Any]]

    let boundary = "Boundary-\(UUID().uuidString)"
    var body = ""
    var error: Error? = nil
    for param in parameters {
      if param["disabled"] == nil {
        let paramName = param["key"]!
        print("params image key is \(paramName)")
        body += "--\(boundary)\r\n"
        body += "Content-Disposition:form-data; name=\"\(paramName)\""
        if param["contentType"] != nil {
          body += "\r\nContent-Type: \(param["contentType"] as! String)"
        }
        let paramType = param["type"] as! String
        if paramType == "text" {
          let paramValue = param["value"] as! String
            print("value of param is \(paramValue)")
          body += "\r\n\r\n\(paramValue)\r\n"
        } else {
        
                if path != nil {
                
                body += "; filename=\"\(path)\"\r\n"
                               + "Content-Type: \"content-type header\"\r\n\r\n \(path)\r\n"
                print("this is going to true")
                }
  }
      }
    }
    body += "--\(boundary)--\r\n";
    let postData = body.data(using: .utf8)

    var request = URLRequest(url: URL(string: "https://api.offernews.co/api/user")!,timeoutInterval: Double.infinity)
    request.addValue("baerar \(profileKeychain["token"]!)", forHTTPHeaderField: "Authorization")
    request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    request.httpMethod = "PATCH"
    request.httpBody = postData

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
      guard let data = data
        
        else {
        print(String(describing: error))
        semaphore.signal()
        return
      }
      print(String(data: data, encoding: .utf8)!)
      semaphore.signal()
    }

    task.resume()
    semaphore.wait()
}

and also this is my another question like this that every code that you want on this link Photo did not update when I patch it to json server swift thanks guys

Ali kazemi
  • 84
  • 7
  • It might be that you need to upload not a path to image, but uiimage converted to base64 string or Data. Is there any information about image upload format in your API? Also is it a typo with word "baerar" -> "bearer"? – Eridana Jul 26 '21 at 06:30
  • @Eridana thanks for attention, the format is jpeg – Ali kazemi Jul 26 '21 at 06:50
  • All I could suggest is you might try to do this: create jpeg data let data = yourImage.jpegData(compressionQuality: 1) and upload this data in field "src" instead of file path. – Eridana Jul 26 '21 at 07:43
  • @Eridana thanks but unfortunately not worked. – Ali kazemi Jul 26 '21 at 08:10
  • Check this https://stackoverflow.com/questions/26335656/how-to-upload-images-to-a-server-in-ios-with-swift – Farrukh Makhmudov Jul 26 '21 at 08:48

0 Answers0