AFNetworking: Send image from file

I am trying to send a multi-page mail request that includes an image. The following code works fine:

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

I want to use appendPartWithFileURLinstead appendPartWithFileData. If I replace the 5th line with the line that is commented out in the above code, I get the following compiler error:

Extra argument 'constructingBodyWithBlock' in call

Does anyone know how to solve this?


edit: a (very, very, very strange) solution was found. Replace line

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

with

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

I did not change anything, adding var temp =. I have no idea why this works, but it does. This seems to be a strange mistake.

+4
source share
2 answers

, casting 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

, 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
0

All Articles