ios-Swift 3 Alamofire分段上传

由于迁移到Swift 3,我发现很难编译使用Alamofire的项目。

上载multipartFormData时发生问题:

Alamofire.upload(.POST, URL, headers: headers, multipartFormData: {
        multipartFormData in
.
.
. 
}) 

对成员'upload(_:to:method:headers :)的含糊引用

任何帮助,不胜感激,在此先感谢!

解决:

 Alamofire.upload(multipartFormData: { (multipartFormData) in

        multipartFormData.append(fileData, withName: "file_pack", fileName: "file_pack", mimeType: "text/plain")


        for (key, value) in self.parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
        }, with: URL2, encodingCompletion: { (result) in

            switch result {
            case .success(let upload, _, _):

                upload.responseJSON { response in
                    self.delegate?.showSuccessAlert()
                    print(response.request)  // original URL request
                    print(response.response) // URL response
                    print(response.data)     // server data
                    print(response.result)   // result of response serialization
                    //                        self.showSuccesAlert()
                    self.removeImage("frame", fileExtension: "txt")
                    if let JSON = response.result.value {
                        print("JSON: \(JSON)")
                    }
                }

            case .failure(let encodingError):
                self.delegate?.showFailAlert()
                print(encodingError)
            }

    })

这就是应该在Swift 3中实现上传方法的方式

DCDC asked 2020-07-14T00:57:16Z
4个解决方案
36 votes

例如,在Swift 3中使用Alamofire 4.0.0:

(确保您已经准备好4.0.0,因为您似乎尚未更新Alamofire)

Alamofire.upload(multipartFormData: { (multipartFormData) in
        // code
    }, to: URL, encodingCompletion: { (result) in
        // code
    })

要么

Alamofire.upload(multipartFormData: { (multipartFormData) in
        // code
    }, with: URL, encodingCompletion: { (result) in
        // code
    })

因此,需要通过URL请求传递headers

let URL = try! URLRequest(url: "http://example.com", method: .get, headers: headers)
pedrouan answered 2020-07-14T00:57:43Z
7 votes

试试这个,将URL设置为@pedrouan说的。

Alamofire.upload(multipartFormData: { (multipartFormData) in
       multipartFormData.append(imageData, withName: "xyz", fileName: "file.jpeg", mimeType: "image/jpeg")
}, to: url) 
{ (result) in
      //result
}
Mitul Marsoniya answered 2020-07-14T00:58:03Z
1 votes

在快速3中,尝试将multipartFormData设置为他的解决方案中指出的@DCDC。 XCode尝试在.data()之前强制转换为AnyObject,因此

value.data(using: String.Encoding.utf8)!, withName: key

我做了

[replace_your_var_name_here].data(using: String.Encoding.utf8)!, withName: key

就我而言,我的var列表并不大,因此可以选择硬编码。

Jorge Manuel Bello answered 2020-07-14T00:58:32Z
1 votes

对于Swift 3和Alamofire〜4.3.0

如果像我这样的人尝试同步获取请求对象(不使用锁或dispatch_groups),则可以使用以下方法:

// outer function
...
let string = "string to send"
let multipartFormData = MultipartFormData()
multipartFormData.append(string.data(using: .utf8)!, withName: "str")

guard let data = try? multipartFormData.encode() else {
    // fail appropriately
}

let request = sessionManager.upload(data,
                                    to: url,
                                    method: .post,
/* this is VERY IMPORTANT LINE */   headers: ["Content-Type" : multipartFormData.contentType])

request.validate()
// do whatever you need with request

请注意,您需要从multipartFormData中设置Content-Type标头,因为它包含边界。

如果您不需要同步您的请求对象,则另一个答案为

Alamofire.upload(multipartFormData: { (multipartFormData) in

正在按预期工作。 如果数据成功编码,它将在回调闭包中返回您的请求对象。

重要说明:如果使用我描述的方法,它将阻止您的线程(在大多数情况下,您可能位于主线程中)复制和编码数据。 因此,请勿将其用于大文件或其他文件。 它故意在Alamofire中不同步。

user1264176 answered 2020-07-14T00:59:14Z
translate from https://stackoverflow.com:/questions/39592085/swift-3-alamofire-multipart-upload