gpt4 book ai didi

swift - 将 Base64 图像上传到服务器时出错

转载 作者:行者123 更新时间:2023-11-30 12:29:33 25 4
gpt4 key购买 nike

当我在 php 脚本中手动添加 base64 图像时,一切都很好,但是当我使用 httpBody 发送它时,所有字段都有效,并且服务器上的 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'];

然后我处理它...除了 $image(base64 字符串)之外的所有字段都有效。

最佳答案

线路

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

缺少“image”后面的第一个 =

关于swift - 将 Base64 图像上传到服务器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43740956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com