我正在使用Wordpress 4.7推出的Wordpress REST Api 我遇到的这个问题只发生一次。 我正在使用swift& Alamofire称为REST Api。
如果我尝试使用http://website.com/wp-json/wp/v2/media端点上传多个图像,它将复制一些图像。
当我检查Wordpress媒体库时,作为重复的图像被命名为完全相同的名称。这是否意味着它是一个WordPress问题?
以下是我正在使用的代码。
WordPressHelper().getAccessCode { (accessString, error) in
if error != nil {
completionHelper(nil, error)
} else {
let cilentsName = "\(jobsite.firstName) \(jobsite.lastName)'s"
let endpoint = "http://website.com/wp-json/wp/v2/media/?access_token=\(accessString)"
let parameters = [
"Content-Disposition": "attachment; filename=appImage.jpg",
"description": "Image from My app",
"media_type": "image"
]
let mgr = Alamofire.SessionManager.default
mgr.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(image, withName: "file", fileName: "\(cilentsName) image.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: .utf8)!, withName: key)
}
}, to: endpoint, encodingCompletion: { (encodingResult) in
switch encodingResult {
case .success(let upload, _, _):
upload.response(completionHandler: { (dataResponse) in
do {
let json = try JSONSerialization.jsonObject(with: dataResponse.data!, options: .allowFragments)
print("success \(json)")
completionHelper(json as? NSDictionary, nil)
} catch let error as NSError{
completionHelper(nil, error)
}
})
case .failure(_):
let error = NSError(domain:"encodingError", code:-1, userInfo:
[NSLocalizedDescriptionKey:"failure trying to upload image."])
print("failure \(error.localizedDescription)")
completionHelper(nil, error)
}
})
}
}