我想用其他一些参数上传图像到服务器。我尝试了很多,但我无法做到。请帮我做。
我的json网址:https://www.zdoof.com/api/zTimeline/insert_post
参数:“Is_checkin”,“描述”,“视频链接”,“Post_image”,“Created_by”,“Created_on”
我的代码:
func jsonToUpload(){ let image = UIImage.init(名称:“IMG_1114.JPG”)
print("Tapan")
let imgData = UIImageJPEGRepresentation(image!, 0.2)!
let param=[
"Is_checkin" : "0",
"Description" : self.cookingText.text!,
"Videolink" : "",
"Post_image" : "",
"Created_by" : "4490",
"Created_on" : "23/Sept/2017"
]
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")
for (key, value) in param{
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to:"https://www.zdoof.com/api/zTimeline/insert_post")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value!)
}
case .failure(let encodingError):
print(encodingError)
}
}
}
我的另一个代码:
func myImageUploadRequest() {
let postPictureUrl = NSURL(string: "https://www.zdoof.com/api/zTimeline/insert_post")
//让Params = scriptUrl +“Created_by =(4490)”+“& Created_on = 01 / jan / 2017”+“& Is_checkin = 0”+“Description =(self.cookingText.text!)” +“Post_image =(myImage.image)”
let request = NSMutableURLRequest(url: postPictureUrl! as URL)
request.httpMethod = "POST"
let param=[
"Is_checkin" : "0",
"Description" : self.cookingText.text!,
"Videolink" : "",
"Post_image" : "" ,
"Created_by" : "4490",
"Created_on" : "20/Sept/2017"
// "mer_type": "smalll"
]
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "application/json")
let imageData = UIImageJPEGRepresentation(myImage.image!, 1)
print(imageData!)
if(imageData==nil) { return; }
//让imageStr = imageData?.base64EncodedString(options:.endLineWithCarriageReturn)
request.httpBody = createBodyWithParameters(parameters: param, filePathKey: "file", imageDataKey: imageData! as NSData, boundary: boundary) as Data
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
// You can print out response object
print("******* response = \(String(describing: response))")
// Print out reponse body
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("****** response data = \(responseString!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
print(json!)
DispatchQueue.main.async(execute: {
self.myImage.image = nil;
});
}catch
{
print(error)
}
}
task.resume()
}
func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(value)\r\n".data(using: String.Encoding.utf8)!)
}
}
let filename = "user-profile.jpg"
let mimetype = "image/jpg"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(imageDataKey as Data)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
扩展名NSMutableData {
func appendString(string: String) {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)
}