2016-10-03 52 views
0

我想從UIImage上傳照片,並且收到錯誤狀態碼500,「statusCode應該是200,但是是500」,有人可以幫忙或者給swift 3上傳一個圖像文件到服務器的另一種解決方案? 或者我如何解決這個使用ALAMOFIRE?上傳照片 - 錯誤狀態代碼500 - swif 3

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() 

回答

0

發生這種情況是因爲Xcode出於安全原因不允許http僅https。它已被解決,包括以下代碼info.plist

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 
相關問題