Apfelsaft
Apfelsaft

Reputation: 5846

AFNetworking: Send image from file

I am trying to send a multi-part post request which 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 appendPartWithFileURL instead of appendPartWithFileData. If I replace the 5th line wiht the line which is commented out in the code above, I get the following compiler error:

Extra argument 'constructingBodyWithBlock' in call

Does anybody know how to resolve this?


edit: Found a (very, very, very strange) solution. Replace the 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 didn't change anything beside adding var temp =. I have no idea why this is working, but it does. Seems to be a strange bug.

Upvotes: 4

Views: 2183

Answers (2)

Glen Selle
Glen Selle

Reputation: 3946

I kept receiving this error until I cast the URL as a string. Here's what I was doing (and receiving the error while doing so):

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

To resolve the issue, I simply changed the assignment of fullUrl to take a string rather than NSURL by adding .absoluteString.

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

Upvotes: 0

Omar Pedraza
Omar Pedraza

Reputation: 43

If you haven't solved this problem yet, try casting location.imagePath to String.

I had the same problem till I've added as String in the following code:

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

Hope it helps.

Upvotes: 1

Related Questions