簡體   English   中英

使用Swift Vapor 3.0上傳文件

[英]File upload using Swift Vapor 3.0

我正在嘗試創建一個簡單的Steam服務,這樣我就可以通過api上傳視頻文件(一次一個)。

從我的應用程序我使用Alamofire上傳視頻文件:

func uploadVideo(video: URL) {
    Alamofire.upload(videoFileURL, to: "http://localhost:8080/upload").responseString { response in
      debugPrint(response)
    }
}

蒸汽控制器方法是這樣的(這是我不知道如何做到的地方):

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";
}

如何從請求中獲取視頻文件並以正確的格式將其寫入磁盤?

方法upload()被正確調用等,因為它只有最后一個return語句才有效。

查看您的功能,您似乎無法正確處理未來的響應或提取數據。

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"
    }
}

看看這是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM