0
當我在php腳本中手動添加base64映像時,一切都很好,但是當我使用httpBody發送它時,所有字段都工作且base64字符串在服務器上爲空。 斯威夫特代碼:將base64映像上傳到服務器時出錯
let requestURL: NSURL = NSURL(string: UrlConstant.InsertItem)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared
urlRequest.httpMethod = "POST"
let postString = "image\(item.imageBase64!)&cityName=\(item.cityName)&shopName=\(item.shopName)&price=\(item.price)&dateExpiring=\(item.dateExpiring)&category=\(item.category)&location=\(item.locationCoords)&u_username=\(item.u_username)&u_id=\(item.u_id)"
urlRequest.httpBody = postString.data(using: .utf8)
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) -> Void in
if let httpResponse = response as? HTTPURLResponse{
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do{
print("status code ok")
}catch{
print("ERROR")
}
}
}else{
print("server not active")
}
}
task.resume()
}
的PHP代碼部分從哪裏獲得這些值:
$cityName = $_POST['cityName'];
$shopName = $_POST['shopName'];
$price = $_POST['price'];
$dateExpiring = $_POST['dateExpiring'];
$category = $_POST['category'];
$location = $_POST['location'];
$u_username = $_POST['u_username'];
$u_id = $_POST['u_id'];
$image = $_POST['image'];
然後我處理它...除了$圖像的(Base64串)的項目工作。
仍然無法正常工作... – Nikola