upload a photo using request.httpMethod = "FILES"

Hi, is there any way to possible upload an image using

request.httpMethod = "FILES"




so i have this code for Select the photo from carrier "photos in Iphone"

but i haven't found the way to store the photo or named it, i only can display it, so i can send it like any other UITextField.text 'info' using my PHP code



@IBAction func SelectPhoto(_ sender: UIButton) {
        let imageController = UIImagePickerController()
        imageController.delegate = self
        imageController.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(imageController, animated: true, completion: nil)
        
        
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        image.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage //display it on UIimageview
        self.dismiss(animated: true, completion: nil)
        
        
    }




this is my code for Upload the photo i'm sending it using request.httpMethod = "FILES"


@IBAction func uploadPhoto(_ sender: UIButton) {
        let request = NSMutableURLRequest(url: NSURL(string: "hehe")! as URL)
        request.httpMethod = "FILES"
        let postString = "Foto=\(!)" // user must choose the photo from carrier
        request.httpBody = postString.data(using: String.Encoding.utf8)
        
        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in
            
            if error != nil {
                print("error=\(String(describing: error))")
                
                
                return
            }
            
            print("response = \(String(describing: response))")
            
            let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
            print("responseString = \(String(describing: responseString))")
            
            
        }
        task.resume()
        
    }



is there any way possible to send it using that method? or should i quit programming help plz 😟

There's no method FILES in HTTP protocol.

I guess you want to receive a file with $_FILES in your PHP side.

In this case, you need to send a request with HTTP protocol POST and Content-Type as multipart/form-data.


Please search with "swift post multipart/form-data" on the web.

Generally, sending a multipart request is a little bit harder than sending a usual request, so if you can construct a basic structure of your code, it becomes easier to help you.

upload a photo using request.httpMethod = "FILES"
 
 
Q