模块“Alamofire”没有名为“upload”的成员
Posted
技术标签:
【中文标题】模块“Alamofire”没有名为“upload”的成员【英文标题】:Module 'Alamofire' has no member named 'upload' 【发布时间】:2019-11-15 14:22:27 【问题描述】:我正在尝试使用 swift 将图像上传到服务器。我试过NSMutableURLRequest
和URLSession
。我收到网络连接丢失。我认为简单地使用Alamofire
会是一种更好的方法,但遇到了一个问题,因为xcode
找不到函数update
。
知道如何使用 Alamofire 上传图片吗?或者找到更新函数?
alamofire 的代码:
func uploadImageWithAlmofire(url: String)
let params: Parameters = ["name": "abcd", "gender": "Male"]
Alamofire.upload(multipartFormData:
(multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.yourimageView.image!, 0.1)!, withName: "file", fileName: "file.jpeg", mimeType: "image/jpeg")
for (key, value) in params
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
, to:url,headers:nil)
(result) in
switch result
case .success(let upload,_,_ ):
upload.uploadProgress(closure: (progress) in
//Print progress
)
upload.responseJSON
response in
//print response.result
if response.result.value != nil
let dict :NSDictionary = response.result.value! as! NSDictionary
let status = dict.value(forKey: "status")as! String
if status=="1"
print("DATA UPLOAD SUCCESSFULLY")
case .failure(let encodingError):
break
【问题讨论】:
你能显示你想使用的代码吗? 添加了用于 alamofire 的代码 【参考方案1】:当您检查Uploading Data to a Server 示例时,它使用AF
而不是Alamofire
:
AF.upload(multipartFormData: multipartFormData in
multipartFormData.append(Data("one".utf8), withName: "one")
multipartFormData.append(Data("two".utf8), withName: "two")
, to: "https://httpbin.org/post")
.responseJSON response in
debugPrint(response)
【讨论】:
【参考方案2】:试试这个
AF.upload(multipartFormData: multipartFormData in
multipartFormData.append(Data(self.businessType.utf8), withName: "business_type")
let theFileName1 = (self.doc1.absoluteString as NSString).lastPathComponent
multipartFormData.append(self.doc1, withName: "key_value", fileName: theFileName1, mimeType: "image/png")
, to: "https://www.test_document.php") //POST URL
.responseJSON response in
debugPrint(response)
【讨论】:
以上是关于模块“Alamofire”没有名为“upload”的成员的主要内容,如果未能解决你的问题,请参考以下文章
Swift - “Alamofire.upload” -> “AF.upload” 上传功能问题
导入 Alamofire 3.4 - 没有这样的模块“Alamofire”
如何使用 Alamofire.upload 函数上传带有一些参数的图片或 wav 文件
PUT NSData 从 S3 到 PreSigned URL,Alamofire.upload(...) 在 iOS 8 上不起作用