将base64映像上载到服务器时出错

时间:2017-05-02 14:50:06

标签: swift

当我在php脚本中手动添加base64图像时,一切都很好,但是当我用httpBody发送它时,所有字段都工作,而base64字符串在服务器上是空的。 Swift代码:

    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字符串)。

1 个答案:

答案 0 :(得分:0)

该行

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

在"图像"。

之后缺少第一个=
相关问题