使用Swift Vapor 3.0上传文件
我正在尝试创建一个简单的vapor服务,这样我就可以通过api上传视频文件(一次一个) 从我的应用程序中,我正在使用Alamofire上传视频文件:使用Swift Vapor 3.0上传文件,swift,alamofire,vapor,Swift,Alamofire,Vapor,我正在尝试创建一个简单的vapor服务,这样我就可以通过api上传视频文件(一次一个) 从我的应用程序中,我正在使用Alamofire上传视频文件: func uploadVideo(video: URL) { Alamofire.upload(videoFileURL, to: "http://localhost:8080/upload").responseString { response in debugPrint(response) } } vapor控制器
func uploadVideo(video: URL) {
Alamofire.upload(videoFileURL, to: "http://localhost:8080/upload").responseString { response in
debugPrint(response)
}
}
vapor控制器的方法是这样的(我不知道怎么做):
如何从请求中获取视频文件并将其转换为正确的格式,以便将其写入磁盘
方法
upload() 查看您的函数,您似乎没有正确处理未来的响应或提取数据
func upload(_ req: Request) throws -> Future<String> {
return try req.content.decode(File.self).map(to: String.self) { (file) in
try file.data.write(to: URL(fileURLWithPath: "/Users/eivindml/Desktop/\(file.filename)"))
return "File uploaded"
}
}
func上传(\req:Request)抛出->未来{
返回try req.content.decode(File.self).map(to:String.self){(File)in
尝试file.data.write(到:URL(fileURLWithPath:“/Users/eivindml/Desktop/\(file.filename)”)
返回“上传的文件”
}
}
看看这是否有帮助 嗯,现在我得到了[ERROR]VaporError.httpDecoder:没有为内容类型:application/octet stream配置HTTPMessageDecoder。
如果内容类型是application/octet stream
,那么您就不需要使用ContentContainer
(req.content
)。这是为了解码来自请求主体的内容,但在本例中,主体只是一个字节流。改用。
func upload(_ req: Request) throws -> Future<String> {
return try req.content.decode(File.self).map(to: String.self) { (file) in
try file.data.write(to: URL(fileURLWithPath: "/Users/eivindml/Desktop/\(file.filename)"))
return "File uploaded"
}
}