模块 'Alamofire' 没有名为 'upload' 的成员

Module 'Alamofire' has no member named 'upload'

我正在尝试使用 swift 将图像上传到服务器。我用 URLSession 试过 NSMutableURLRequest。我收到网络连接丢失。我认为简单地使用 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
            }
        }
    }

当您检查 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)
    }

试试这个

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)
        }