我正在使用以下代码,图片无法上传。使用参数上传图像文件。我正在使用post方法
let uploadData = NSMutableData()
if let image = self.imgProfile.image {
let imageData = UIImageJPEGRepresentation(image, 1.0)
// create upload data to send
let boundaryConstant = "----------------12345";
// add image
uploadData.append("\r\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)
uploadData.append("Content-Disposition: form-data; name=\"picture\"; filename=\"file.png\"\r\n".data(using: String.Encoding.utf8)!)
uploadData.append("Content-Type: image/png\r\n\r\n".data(using: String.Encoding.utf8)!)
uploadData.append(imageData!)
uploadData.append("\r\n--\(boundaryConstant)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = uploadData as Data
}
print("uploadadata\(uploadData)")
let postString:String = "user_id=\(5)&name=\( self.nameTextField.text!)&lastname=\(self.lastnameTextField.text!)&city=\(city)&state=\(state)&country=\(country)&address=\(self.addressTextField.text!)&zipcode=\(zipInt)))&adventure_style=\(self.adventureTextField.text!)&image=\(uploadData)"
print("postString\(postString)")
// let postString = "phone=\(phno!)&password=\(passwordTextField.text!)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
print(jsonResult)
let status = jsonResult["status"]! as! NSString
print("status\(status)")
if status == "0" {
let message = jsonResult["message"]! as! String
print("message\(message)")
self.removeActivityIndicator(activityIndicator: myActivityIndicator)
}
else {
print("save successfull")
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
self.removeActivityIndicator(activityIndicator: myActivityIndicator)
}
我想在我的网址上传图片文件。上传图片时没有上传图片。当我检索图片时不显示新图片。图像如何上传图像。请帮帮我!