上传照片 - 错误状态代码500 - swift 3

时间:2016-10-03 23:07:21

标签: httprequest swift3 image-uploading alamofire

我正在尝试从UIImage上传照片,并且我收到错误状态码500," statusCode应该是200,但是500",有人可以帮忙或者可能提供另一个上传解决方案一个图像文件到swift 3服务器? 或我如何使用ALAMOFIRE来解决这个问题?

let imageData = UIImageJPEGRepresentation(self.images.firstObject as! UIImage, 0.9)

let myBase64Data = imageData!.base64EncodedString(options: [])

let request = NSMutableURLRequest(url: URL(string: "http://www.example.com/members/mobileapi/uploadSingleFile/")!)
request.httpMethod = "POST"
let postString = "username=tiagotest&password=test&bpo_id=248522&encoded_string=\(myBase64Data)&image_name=testFromIphone"

request.httpBody = postString.data(using: String.Encoding.utf8)

let task = URLSession.shared.dataTask(with: request as URLRequest) {

    (data, response, error) in
    guard error == nil && data != nil else {                                                              
        print("error=\(error)")
        return
    }

    if let httpStatus = response as? HTTPURLResponse , httpStatus.statusCode != 200 {
        print("statusCode should be 200, but is \(httpStatus.statusCode)")
        print("response = \(response)")
    }

    let responseString = String(data: data!, encoding: String.Encoding.utf8)
    print(responseString!)
}
task.resume()

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为出于安全原因,Xcode不允许仅使用http http。它已解决,包括info.plist中的代码

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>