2017-12-26 28 views
0

ポストマンに、我々はここではバイナリ

enter image description here

の下にそうであるように、私は、バイナリとして画像をアップロードすると画像をアップロードする方法その要求がないようです私のコード

var url = myURLString 
url = url.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)! 

guard let imageData = UIImageJPEGRepresentation(image, 0.4) else { 
     return 
    } 

request.httpBody = imageData 
request.httpMethod = "POST" 
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 

Alamofire.request(request).responseJSON { (response) in 
     if let JSON = response.result.value as? NSDictionary { 
      print(JSON) 
     } else { 
      let message = response.result.error != nil ? response.result.error!.localizedDescription : "Unable to communicate." 
      print(message) 
     } 
} 

です画像ファイルを添付して、次のエラーメッセージを返す

"レスポンスをシリアル化できませんでした。入力データはゼロまたは長さゼロでした。

+1

理由だけではなく、 'Alamofire.upload(...)'を使わないのでしょうか? – user28434

+0

[alamofire.error Code = -6006 "JSONをシリアル化できませんでした]の重複している可能性があります(https://stackoverflow.com/questions/35374798/alamofire-error-code-6006-json-could-not-be-serialized) – AshvinGudaliya

+0

'responseJSON'ではなく' responseString'を試して、どんなエラーが出ているのか確認してください。 –

答えて

0

私はギャラリーから画像を検索し、コードの下に使用することで、画像の名前を取っています: あなたは、以下の機能を使用することができます。迅速な3については

func createMultipart(image: UIImage, callback: Bool -> Void){ 
    // use SwiftyJSON to convert a dictionary to JSON 
    var parameterJSON = JSON([ 
     "id_user": "test" 
    ]) 
    // JSON stringify 
    let parameterString = parameterJSON.rawString(encoding: NSUTF8StringEncoding, options: nil) 
    let jsonParameterData = parameterString!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) 
    // convert image to binary 
    let imageData = UIImageJPEGRepresentation(image, 0.7) 
    // upload is part of AlamoFire 
    upload(
     .POST, 
     URLString: "use your url here", 
     multipartFormData: { multipartFormData in 
      // fileData: puts it in "files" 
      multipartFormData.appendBodyPart(fileData: jsonParameterData!, name: "goesIntoFile", fileName: "json.txt", mimeType: "application/json") 
      multipartFormData.appendBodyPart(fileData: imageData, name: "file", fileName: "ios.jpg", mimeType: "image/jpg") 
      // data: puts it in "form" 
      multipartFormData.appendBodyPart(data: jsonParameterData!, name: "goesIntoForm") 
     }, 
     encodingCompletion: { encodingResult in 
      switch encodingResult { 
      case .Success(let upload, _, _): 
       upload.responseJSON { request, response, data, error in 
        let json = JSON(data!) 
        println("json:: \(json)") 
        callback(true) 
       } 
      case .Failure(let encodingError): 
       callback(false) 
      } 
     } 
    ) 
} 

let fotoImage = UIImage(named: "foto") 
    createMultipart(fotoImage!, callback: { success in 
    if success { } 
}) 
0

、コードの下Alamofire 4 が正常に動作します

var url = myURLString 
url = url.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)! 
guard let imageData = UIImageJPEGRepresentation(image, 0.4) else { 
    return 
} 

Alamofire.upload(imageData, to: URL(string: url)!, method: .post, headers: nil).responseJSON { (response) in 
    if let JSON = response.result.value as? NSDictionary { 
     print(JSON) 
    } else { 
     let message = response.result.error != nil ? response.result.error!.localizedDescription : "Unable to communicate." 
     print(message) 
    } 
}