0

I have to upload any kind of file system from local phone memory to API given.Is there a simple way to that please help.

chintu
  • 9
  • 1
  • Please provide a code sample of what you have tried.. So that we can determine exactly what is not working. – Jonathan Alfaro Nov 25 '19 at 18:45
  • Possible duplicate of [How to upload images to a server in iOS with Swift?](https://stackoverflow.com/questions/26335656/how-to-upload-images-to-a-server-in-ios-with-swift) – Mohammad Reza Koohkan Nov 25 '19 at 18:49

1 Answers1

0

You can use Alamofire library. https://github.com/Alamofire/Alamofire

Alamofire.upload(multipartFormData: { (multipartFormData) in
    if let parameters = parameters {
        for (key, value) in parameters {
            if let value = value as? String {
                multipartFormData.append(value.data(using: .utf8)!, withName: key)
            }else if let _image = value as? UIImage, let image = _image.resized(toWidth: 500) {
                if let jpegData = image.jpegData(compressionQuality: 0.5) {
                    multipartFormData.append(jpegData, withName: key, fileName: "post.jpeg", mimeType: "image/jpeg")
                }else if let pngData = image.pngData() {
                    multipartFormData.append(pngData, withName: key, fileName: "post.png", mimeType: "image/jpeg")
                }
            }else {
                multipartFormData.append(Data(from: value), withName: key)
            }
        }
    }
}, usingThreshold: UInt64.init(), to: urlString, method: method, headers: headers, encodingCompletion: { (result) in
    switch result {
    case .success(let upload, _, _):
        upload.responseJSON { response in
            if let responseData = response.result.value as? NSDictionary {
                completion(responseData, response.data, response.result)
            }else if let responseData = response.result.value as? [String: Any] {
                completion(responseData as NSDictionary, response.data, response.result)
            }else {
                completion([:], nil, response.result)
            }
        }
        break
    case .failure(let encodingError):
        completion([:], nil, .failure(encodingError))
        break
    }
})
AMIT
  • 906
  • 1
  • 8
  • 20