2

so my doubt is on how to upload an image using Alamofire, from what researched people uses the solutions here https://github.com/Alamofire/Alamofire/issues/110. However my curl request should look like this:

curl -X POST "api.local.app.com:9000/1/media/upload" -F “picture=@filename.png” -H “Authorization: Alpha ahudhasiadoaidjiajdiudaiusdhuiahdu” -v

I'm trying to do that this way:

Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization" : "Alpha \(userToken)" ]
        var fileManager = NSFileManager()
        var tmpDir = NSTemporaryDirectory()
        let filename = "tempPicture.png"
        let path = tmpDir.stringByAppendingPathComponent(filename)
        var error: NSError?
        let imageData = UIImagePNGRepresentation(image)
        fileManager.removeItemAtPath(path, error: nil)
        println(NSURL(fileURLWithPath: path))
        if(imageData.writeToFile(path,atomically: true)){
            println("Image saved")
        }else{
            println("Image not saved")
        }

        Alamofire.upload(.POST, databaseURL + "/media/upload", NSURL(fileURLWithPath: path)!).progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
            println( String(totalBytesWritten) + "/" + String(totalBytesExpectedToWrite))
            }
            .responseJSON { (request, response, data, error) in

From what I understand this should work however the Alamo returns

Error: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 2.) UserInfo=0x7e16d9e0 {NSDebugDescription=Invalid value around character 2.}).

  • Check this thread out. http://stackoverflow.com/questions/26121827/uploading-file-with-parameters-using-alamofire – Tobias May 02 '15 at 03:10
  • Also this might be helpful: https://medium.com/@creativewithin/uploading-a-file-in-swift-via-post-multipart-form-data-93dd1001f9e5 – Tobias May 02 '15 at 03:13
  • Thx everyone, with the help of every answer I figure it out that the problem was on me when creating the header. – Matheus Rosendo Pedreira May 25 '15 at 23:53

1 Answers1

-1

I have already provided a detail answer here. Most likely your server expects the image to be multipart form data encoded which is why it is getting rejected. There are many details provided in that link, along with next steps.

Community
  • 1
  • 1
cnoon
  • 16,575
  • 7
  • 58
  • 66