swift,使用alamofire将图像上传到php服务器

时间:2016-02-25 08:05:05

标签: ios iphone swift alamofire

我使用此代码上传带alamofird的照片:

Alamofire.upload(.POST, Url, multipartFormData: {
            multipartFormData in
            if let Data = UIImagePNGRepresentation(imageData) {
                multipartFormData.appendBodyPart(data: Data, name: "file", fileName: filename+".png", mimeType: "image/png")
                //print(Data)
            }
            multipartFormData.appendBodyPart(data: sessionId.dataUsingEncoding(NSUTF16LittleEndianStringEncoding)!, name: "session_id")
            multipartFormData.appendBodyPart(data: NSUserDefaults.standardUserDefaults().stringForKey("user_id")!.dataUsingEncoding(NSUTF8StringEncoding)!, name: "sender_id")

            }, encodingCompletion: {
                encodingResult in
                switch encodingResult {
                case .Success(let upload, _, _):
                    upload.responseJSON { response in
                        print(response)
                        //var data = JSON(response.result.value!)
                    }
                case .Failure(let encodingError):
                    print(encodingError)
                }
        })

我有以下错误:

  

FAILURE:Error Domain = NSCocoaErrorDomain Code = 3840“字符0周围的值无效。” UserInfo = {NSDebugDescription =周围的值无效   字符0。}

从前一天开始,这段代码正在运行

1 个答案:

答案 0 :(得分:3)

只需更换

即可
multipartFormData.appendBodyPart(data: NSUserDefaults.standardUserDefaults().stringForKey("user_id")!.dataUsingEncoding(NSUTF8StringEncoding)!, name: "sender_id")

此构造函数的构造函数方法

 multipartFormData.appendBodyPart(data: image1Data, name: "post_img0", fileName: fileName!, mimeType: "image/png")

实施例

let userID = NSUserDefaults.standardUserDefaults().integerForKey("UserID")
        let userData = String("\(userID)").dataUsingEncoding(NSUTF8StringEncoding)

        var image1Data : NSData!
        if(self.fecalImageView.image == nil ){
        }else{
            image1Data = UIImageJPEGRepresentation( self.fecalImageView.image!, 0.5)!
        }
        Alamofire.upload(
            .POST,
            "http://www.imagesample.php",
            multipartFormData: { multipartFormData in

                 multipartFormData.appendBodyPart(data: userData!, name: "user_id")
                if(image1Data == nil){

                }else{
                    multipartFormData.appendBodyPart(data: image1Data, name: "post_img0", fileName: fileName!, mimeType: "image/png")
                }
            },
            encodingCompletion: { encodingResult in
                switch encodingResult {

                case .Success(let upload, _, _):
                    upload.responseJSON { response in

                        if let dict = response.result.value {
                            let msg = dict["msg"]!


                            if msg!.isEqualToString("sample Added successfully") {


                            }
                        }
                    }
                case .Failure(let encodingError):

                    print(encodingError)
                }
            }
        )

这将解决你的问题。这个帮助。