ios - 如何将图像、pdf、音频、视频和任何文件系统从 iphone 本地内存上传到服务器(API)
问题描述
我必须将任何类型的文件系统从本地手机内存上传到给定的 API。有没有简单的方法请帮忙。
解决方案
您可以使用 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
}
})
推荐阅读
- java - 如果可以的话,我可以在 android studio 中使用数组吗?那么如何在屏幕上显示数组的所有元素
- keras - Keras LSTM 网络,Softmax/Embedding 输出作为每个时间步的输入(时间步 0 除外)
- java - 如何添加
清单中的标签? - mongodb - 如何根据 _id 字段合并 $facet 结果?
- openstack - 无法启动 OpenStack Glance-api
- ruby-on-rails - 渲染电话:与 CommonMarker 的链接
- mongodb - 父集合引用了父数组,如何填充它?
- sql - 将 SQL 转换为 createQueryBuilder
- php - sqlite 数据库:获取按 sum(col2) 排序的 col1 中项目的每小时计数
- java - 使用位置参数 Java 将公历日期转换为回历日期