0

I use this method to post data to my java servlet, but getParameter("2") is null, but I can still get the image.

 let dics = NSMutableDictionary()
    dics.setObject(1, forKey: "2")
    dics.setObject(2, forKey: "3")
    manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>

    manager.POST(urlstr, parameters: dics, constructingBodyWithBlock: {
        fromData in
        fromData.appendPartWithFormData(imgdata, name: "userImg")
    }, success: { (operation, responseObject)in
        println("bingo")
    }) { (operation, error) -> Void in
        println(error)
    }

My question is:

  1. How can I get the parameters?

  2. and I want to know appendPartWithFormData(imgdata, name: "userImg"),how should I use the "userImg" ?

I use request.getInputStream() to get the image..

Saikiran Komirishetty
  • 6,525
  • 1
  • 29
  • 36
MING W
  • 1
  • 3
  • Which parameters and where? You posted a code for AFNetworking, but it seems like you're asking for how to get it in Java on the server side? – Michal Aug 06 '15 at 11:01

3 Answers3

0

Try Alamofire framework, i am sure to solve your problem.

You can upload multipart form data like this;

Alamofire.upload(
.POST,
URLString: "http://httpbin.org/post",
multipartFormData: { multipartFormData in
    multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "userImg")

},
encodingCompletion: { encodingResult in
    switch encodingResult {
    case .Success(let upload, _, _):
        upload.responseJSON { request, response, JSON, error in
            println(JSON)
        }
    case .Failure(let encodingError):
        println(encodingError)
    }
}

)

SukruK
  • 560
  • 2
  • 8
  • 22
  • !I'll try that.But I still don't know the meaning of the parameter "userImg" in appendBodyPart.And how I use this in server side?Can u explain it? :)thanks a lot!! – MING W Aug 06 '15 at 15:22
0

I have solved this problem! :^)

The way I post to server is correct,but the way I get the parameter is wrong. Not a problem about the iOS or AFN.

Because I post multi form data to server,so I cant just use request.getParameter("2") to get the parameter.

And the parameter "name" in appendPartWithFormData(imgdata, name: "userImg")is to separate the data from other data.

For how to get the multipart/form-data parameters in a Servlet,please click here:get multipart/form-data parameters in servlet

Community
  • 1
  • 1
MING W
  • 1
  • 3
-1

Upload image using AFNetworking in Swift 4

func uploadUsingPOST_Image (urlPath: NSString, withParameter dictData: NSMutableDictionary, withImage image: UIImage, WithImageName imageName: NSString,successBlock success:@escaping (_ responceData:AnyObject)->Void) {

    let manager = AFHTTPRequestOperationManager()

    var Timestamp: String {
        return "\(NSDate().timeIntervalSince1970 * 1000)"
    }

    let operation =  manager.post(urlPath as String, parameters: dictData, constructingBodyWith: { (data:AFMultipartFormData!) -> Void in

        if image != nil {

            data.appendPart(withFileData: UIImagePNGRepresentation(image!)!, name: imageName as String, fileName: "\(Timestamp).png", mimeType: "image/png")


        }
    }, success: { (operation, responseObject) -> Void in

        success(responseObject as AnyObject)
    }) { (operation, error) -> Void in

        print(error, terminator: "")
    }

    operation?.start()
}