相关疑难解决方法(0)

使用Alamofire上传带参数的文件

我正在尝试使用上传文件Alamofire.使用File(NSUrl)时上传工作正常,但是,我似乎无法弄清楚如何使用该NSData选项?

这就是我的测试:

 var url:NSURL = NSURL.URLWithString("http://localhost:8080/bike.jpeg")

 var err: NSError?
 var imageData :NSData = NSData.dataWithContentsOfURL(url,options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)

 Alamofire.upload(.POST, "http://localhost:8080/rest/service/upload/test.png", imageData)
        .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
            println(totalBytesWritten)
        }
        .responseJSON { (request, response, JSON, error) in
            println(request)
            println(response)
           println(JSON)
 }
Run Code Online (Sandbox Code Playgroud)

我收到状态代码415?

另外,如何在上传中发送其他参数?

谢谢

编辑

我没有设置正确的内容类型:

var manager = Manager.sharedInstance
manager.session.configuration.HTTPAdditionalHeaders = ["Content-Type": "application/octet-stream"]


let imageData: NSMutableData = NSMutableData.dataWithData(UIImageJPEGRepresentation(imageTest.image, 30));

Alamofire.upload(.POST, "http://localhost:8080/rest/service/upload?attachmentName=file.jpg",  imageData)
        .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
            println(totalBytesWritten)
        }
        .responseString { (request, response, JSON, …
Run Code Online (Sandbox Code Playgroud)

ios swift alamofire

93
推荐指数
4
解决办法
8万
查看次数

标签 统计

alamofire ×1

ios ×1

swift ×1