【问题标题】:Upload photos - Error status code 500 - swift 3上传照片 - 错误状态代码 500 - swift 3
【发布时间】:2016-10-03 23:07:21
【问题描述】:

我正在尝试从 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()

【问题讨论】:

    标签: httprequest swift3 image-uploading alamofire


    【解决方案1】:

    发生这种情况是因为出于安全原因 Xcode 不允许 http 只有 https。它已解决,包括 info.plist 中的以下代码

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

    【讨论】:

      猜你喜欢
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 2011-10-13
      • 2012-03-30
      • 2015-07-06
      • 2011-07-07
      • 1970-01-01
      相关资源
      最近更新 更多