【发布时间】:2019-09-24 17:33:15
【问题描述】:
由于我是iOS新手,在这里停留了一段时间,我需要将图像上传到具有键和值的服务器(“文件”:图像),在邮递员中找到附加的图像。
How to upload images to a server in iOS with Swift?,Upload image to server - Swift 3Upload image to server - Swift 3 几乎所有的建议我都试过了
这里我尝试了一些东西,但没有得到输出响应,因为请求中没有传递密钥
let url = URL(string: uploadurl);
let request = NSMutableURLRequest(url: url!);
request.httpMethod = "POST"
let boundary = "Boundary-\(NSUUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData = UIImageJPEGRepresentation(image, 1)
if (imageData == nil) {
print("UIImageJPEGRepresentation return nil")
return
}
let body = NSMutableData()
//here I need to pass the data as ["file":image]
body.append(imageData!)
request.httpBody = body as Data
let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if let data = data {
// do
let json = try!JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary
print("json value \(json)")
} else if let error = error {
print(error.localizedDescription)
}
})
task.resume()
请建议我,如何将这些图像作为 ["file": image] 传递给正文。
提前致谢
【问题讨论】:
-
您没有显示代码中最重要的部分,因此我们无法说出哪里出了问题。除了您的服务器如何接收
file之外,请显示您的完整代码和服务器响应。 -
drive.google.com/open?id=1xmLZHEnID22LS2P4H6hFcLIhM-bF4-mh ,请找到截图,这是我所期望的,感谢您的回复
-
您最好在问题文本中包含尽可能多的信息,以便尽快获得更好的答复。您知道您可以编辑自己的问题。
-
@Swift_prasad 你应该看看这篇文章:stackoverflow.com/questions/26335656/…
-
@Swift_prasad 我建议最好将
Alamofire用于多部分。 stackoverflow.com/questions/40519829/…
标签: ios swift nsurlrequest