简体   繁体   English

如何将图像、pdf、音频、视频和任何文件系统从 iphone 本地内存上传到服务器(API)

[英]how to upload image,pdf,audio,video and any file system from iphone local memory to server(API)

我必须将任何类型的文件系统从本地手机内存上传到给定的 API。有没有简单的方法可以帮助。

You can use Alamofire library.您可以使用 Alamofire 库。 https://github.com/Alamofire/Alamofire 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
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM