AFNetworking:从文件发送图像

时间:2014-10-14 09:37:50

标签: ios swift afnetworking-2

我正在尝试发送包含图片的多部分帖子请求。以下代码工作正常:

 manager.POST( apiUrl + "/location/add",
        parameters: parameters,
        constructingBodyWithBlock: { (formData : AFMultipartFormData!) -> Void in
          //  formData.appendPartWithFileURL(NSURL(string: location.imagePath!), name: "image", error: nil)},
            formData.appendPartWithFileData(img, name: imgParam, fileName: "randomimagename.jpg", mimeType: "image/jpeg")},
        success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
            println("JSON: " + responseObject.description)
            var dict = responseObject as NSDictionary
            let json = JSONValue(dict)

            var message = ""
            if let msg = json["message"].string {message = msg}
            var success = false
            if let s = json["success"].bool {
                callback(success: success, msg: message)
            }
        },
        failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
            println("Error: " + error.localizedDescription)
            var apiError = ApiError()
            apiError.noConnection = true
            errorCallback(apiError: apiError)
    })

我想使用appendPartWithFileURL代替appendPartWithFileData。如果我用上面代码中注释掉的行替换第5行,我得到以下编译器错误:

Extra argument 'constructingBodyWithBlock' in call

有人知道如何解决这个问题吗?


编辑:找到一个(非常非常非常奇怪的)解决方案。替换

formData.appendPartWithFileURL(NSURL(string: location.imagePath!), name: "image", error: nil)},

var temp = formData.appendPartWithFileURL(NSURL(string: location.imagePath!), name: "image", error: nil)},

除了添加var temp =之外,我没有更改任何内容。我不知道为什么这有效,但确实如此。似乎是一个奇怪的错误。

2 个答案:

答案 0 :(得分:1)

如果尚未解决此问题,请尝试将location.imagePath转换为String。

在我在以下代码中添加as String之前,我遇到了同样的问题:

func uploadFile(file: NSData, url: String, formData: NSDictionary, parameters: NSDictionary, completion: AnyObject -> Void, failure: NSError -> Void) {
    operationManager.requestSerializer = AFJSONRequestSerializer() as AFHTTPRequestSerializer

    if operationManager.reachabilityManager.networkReachabilityStatus != .NotReachable {
        operationManager.POST(url, parameters: parameters, constructingBodyWithBlock: { (data) in
            data.appendPartWithFileData(file, name: formData["fileName"] as String, fileName: formData["fileName"] as String, mimeType: formData["mimeType"] as String)
        }, success: { (operation, responseObject) in
            completion(responseObject)
        }, failure: { (operation, error) in
            failure(error)
        })
    } else {
        showReachabilityAlert()
    }
}

希望它有所帮助。

答案 1 :(得分:0)

我一直收到此错误,直到我将URL转换为字符串。这就是我正在做的事情(并在这样做时收到错误):

let manager = AFHTTPSessionManager()
let fullUrl = NSURL(string: name, relativeToURL: NSURL(string: apiBaseEndpoint))

manager.POST(fullUrl, parameters: nil, constructingBodyWithBlock: { (formData) in
    formData.appendPartWithFormData(file, name: "image")
}, success: { (operation, responseObject) in
    NSLog("hit success")
}, failure: { (operation, error) in
    NSLog("hit the error")
})

要解决此问题,我只需将fullUrl的分配更改为使用字符串而不是NSURL添加.absoluteString

let fullUrl = NSURL(string: name, relativeToURL: NSURL(string: apiBaseEndpoint)).absoluteString