我试图模仿以下的卷曲:
curl -v -F file=@/Users/myuser/Downloads/shelly-homekit-Shelly25.zip http://10.0.1.7/update为了使用curl命令,我下载了zip文件并将其保存到我的计算机上。
我的应用程序应该下载压缩文件,将其存储到设备上,然后上传到服务器。
我尝试将其上传为文件和数据,但都没有成功:
let destination: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("shelly-homekit-Shelly1PM.zip")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download("https://rojer.me/files/shelly/shelly-homekit-Shelly1PM.zip", to: destination).response { response in
debugPrint(response)
if response.error == nil, let zipPath = response.fileURL?.path {
let url = URL(string: zipPath)!
let headers: HTTPHeaders = [
.contentType("multipart/form-data")
]
if let data = try? Data(contentsOf: url) {
AF.upload(data, to: "http://"+dev.ipAddress+"/update",headers: headers).responseDecodable(of: HTTPBinResponse.self) { response in
debugPrint(response)
}
}
}
}我得到以下错误:

谢谢你的帮助
发布于 2021-08-24 14:28:27
以下代码起作用:
let data = try? NSData(contentsOf: URL(string:str)!) as Data
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(data!,withName: "file",fileName: file_name ,mimeType: "application/octet-stream")
}, to: "http://"+dev.ipAddress+"/update")
.response { response in
if response.response?.statusCode != 200 {
devicesUpdateError.append(dev.name)
}
}发布于 2021-08-21 02:21:49
正如注释中提到的那样,您的方法是允许任意加载。见传输安全阻止了明文HTTP。想了解更多细节
发布于 2021-08-22 07:04:36
这没什么用:
正如注释中提到的那样,您的方法是允许任意加载。请参阅传输安全性阻止了明文HTTP。想了解更多细节
我设置了链接中提到的权限:
但是得到以下错误:
我还对我的代码做了一些修改:
let destination: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("shelly-homekit-Shelly1PM.zip")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download("https://rojer.me/files/shelly/shelly-homekit-Shelly1PM.zip", to: destination).response { response in
//debugPrint(response)
if response.error == nil {
AF.upload(response.fileURL!, to: "http://"+dev.ipAddress+"/update").responseDecodable(of: HTTPBinResponse.self) { response_up in
debugPrint(response_up)
}
}
}https://stackoverflow.com/questions/68867084
复制相似问题