2017-05-02 75 views
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串)的项目工作。

回答

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

缺少第一=正确的 “形象” 了。

+0

仍然无法正常工作... – Nikola