[英]File upload using Swift Vapor 3.0
I'm trying to create a simple vapor service so I can upload video files (one at a time) through an api. 我正在尝试创建一个简单的Steam服务,这样我就可以通过api上传视频文件(一次一个)。
From my app I'm uploading the video file using Alamofire: 从我的应用程序我使用Alamofire上传视频文件:
func uploadVideo(video: URL) {
Alamofire.upload(videoFileURL, to: "http://localhost:8080/upload").responseString { response in
debugPrint(response)
}
}
The vapor controller method is like this (this is where I don't know how to do it): 蒸汽控制器方法是这样的(这是我不知道如何做到的地方):
func upload(_ req: Request) throws -> String {
let data = try req.content.decode(Data.self).map(to: Data.self) { video in
try Data(video).write(to: URL(fileURLWithPath: "/Users/eivindml/Desktop/video.mp4"))
return Data(video)
}
return "Video uploaded";
}
How do I get the video file from the request and into the right format so I can write it do disk? 如何从请求中获取视频文件并以正确的格式将其写入磁盘?
The method upload()
is called correctly etc, as it works if I just have the last return statement. 方法
upload()
被正确调用等,因为它只有最后一个return语句才有效。
Looking at your function it appears you're not handling your future response correctly, or extracting the data. 查看您的功能,您似乎无法正确处理未来的响应或提取数据。
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"
}
}
See if this helps. 看看这是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.