Ios &引用;UTF-8中的字节序列无效;使用Alamofire上载图像时

Ios &引用;UTF-8中的字节序列无效;使用Alamofire上载图像时,ios,swift,alamofire,Ios,Swift,Alamofire,我可以毫无问题地将图像发布到rails应用程序: curl -v include --form image=@filename.jpg https://my_url_goes_here.com/path/to/imageupload …但是当我尝试使用Alamofire.upload上载时,我在服务器上看到“UTF-8中的字节序列无效”错误,并且没有保存任何内容 “图像”是有效的UIImage let pictureData = UIImageJPEGRepresentation(i

我可以毫无问题地将图像发布到rails应用程序:

 curl -v include --form image=@filename.jpg https://my_url_goes_here.com/path/to/imageupload
…但是当我尝试使用Alamofire.upload上载时,我在服务器上看到“UTF-8中的字节序列无效”错误,并且没有保存任何内容

“图像”是有效的UIImage

    let pictureData = UIImageJPEGRepresentation(image!, 0.8)

    Alamofire.upload(.POST, "https://my_url_goes_here.com/path/to/imageupload", pictureData)
        .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
            println(totalBytesWritten)
        }
        .responseJSON { (request, response, JSON, error) in
            println(JSON)
    }
}

Alamofire.upload应该将NSData作为参数(并在尝试上载之前对其进行适当编码),因此我不确定这里出了什么问题。

FWIW,我应该更仔细地阅读Alamofire文档——我尝试进行的文件上载需要作为多部分数据,这。。。因此,在这里使用AFNetworking及其内置的多部分表单上传支持是可行的。

除非图像数据在Base64中,它不可能是UTF8。您如何指定MIME类型?@hotlick有两个优点——尽管我希望Alamofire.upload至少能够自动将NSData编码到Base64中,以便安全地上载。我还希望它能适当地处理MIME类型,但也许它不能——我会深入挖掘。谢谢你的想法。一般来说,你可以使用不需要Base64编码的“批量数据”协议上传图像。我不知道如何用Alamofire指定它。不知何故,您应该在HTTP头中指定“Content-Type:image/jpeg”。