当前位置: 首页 > 知识库问答 >
问题:

如何使用蒸汽3为iOS应用程序上传模型文件

许出野
2023-03-14

我想使用Vapor 3作为后端制作iOS应用程序。我创建的用于表示对象的模型包含一些属性,这些属性将是文件,例如。png和。plist文件。我很难理解如何使用multipart来获取这些文件,并在执行POST请求时将它们发送到我的模型endpoint。

我还不清楚应该在模型类中设置哪些文件属性的数据类型。在多部分文档中(https://docs.vapor.codes/3.0/multipart/overview/#content)在“内容”部分下,他们说要创建一个结构,并将其图像设置为数据类型,但也说可以将其设置为文件类型。我还看到了一些示例,其中他们将其设置为String类型。

我希望有人能澄清我应该将这些属性的数据类型设置为什么,以及如何将这些文件上载到我保存和调用的控制器/模型控制器中。我的引导(路由器:路由器)函数中的post()

我已经阅读了多部分vapor文档并阅读了这些stackoverflow帖子,但仍然不明白当我尝试使用post方法时应该做什么:-vapor一次上载多个文件-如何使用vapor 3处理多部分请求-使用PostgreSQL在vapor 3中上载图像

这是我的模型类:

import Vapor
import FluentMySQL

final class AppObject: Codable {
  var id: Int?
  var plistFile: String // file
  var imageFile: String // file
  var notes: String
  var name: String

  init(ipaFile: String, plistFile: String, imageFile: String, notes: String, name: String) {
    self.ipaFile = ipaFile
    self.plistFile = plistFile
    self.imageFile = imageFile
    self.notes = notes
    self.name = name
  }
}

extension AppObject: MySQLModel {}
extension AppObject: Content {}
extension AppObject: Migration {}
extension AppObject: Parameter {}

这是我对上述模型的控制器:

import Vapor
import Fluent

struct AppObjectsController: RouteCollection {


    func boot(router: Router) throws {
        let appObjectsRoute = router.grouped("api", "apps")
        appObjectsRoute.get(use: getAllHandler)
        appObjectsRoute.post(AppObject.self, use: createHandler)
    }

    func getAllHandler(_ req: Request) throws -> Future<[AppObject]> {
        return AppObject.query(on: req).all()
    }

    // what else should I be doing here in order to upload actual files?
    func createHandler(_ req: Request, appobject: AppObject) throws -> Future<AppObject> {
         return appobject.save(on: req)
    }
}

我看到的一些例子涉及到为web应用程序上传,它们返回了一个未来

请帮帮忙,我尽力把这个字写好,我是新来的Vapor

共有1个答案

孙德宇
2023-03-14

模型

final class AppObject: Codable {
    var id: Int?
    var ipaFile: String // relative path to file in Public dir
    var plistFile: String // relative path to file in Public dir
    var imageFile: String // relative path to file in Public dir
    var notes: String
    var name: String

    init(ipaFile: String, plistFile: String, imageFile: String, notes: String, name: String) {
        self.ipaFile = ipaFile
        self.plistFile = plistFile
        self.imageFile = imageFile
        self.notes = notes
        self.name = name
    }
}

extension AppObject: MySQLModel {}
extension AppObject: Content {}
extension AppObject: Migration {}
extension AppObject: Parameter {}

控制器

struct AppObjectsController: RouteCollection {
    func boot(router: Router) throws {
        let appObjectsRoute = router.grouped("api", "apps")
        appObjectsRoute.get(use: getAllHandler)
        appObjectsRoute.post(PostData.self, use: createHandler)
    }

    func getAllHandler(_ req: Request) throws -> Future<[AppObject]> {
        return AppObject.query(on: req).all()
    }
}

extension AppObjectsController {
    struct PostData: Content {
        let ipaFile, plistFile, imageFile: File
        let name, notes: String
    }

    func createHandler(_ req: Request, payload: PostData) throws -> Future<AppObject> {
        let ipaFile = ServerFile(ext: "ipa", folder: .ipa)
        let plistFile = ServerFile(ext: "plist", folder: .plist)
        let imageFile = ServerFile(ext: "jpg", folder: .image)
        let appObject = AppObject(ipaFile: ipaFile.relativePath, plistFile: plistFile.relativePath, imageFile: imageFile.relativePath, notes: payload.notes, name: payload.name)
        /// we have to wrap it in transaction
        /// to rollback object creating
        /// in case if file saving fails
        return req.transaction(on: .mysql) { conn in
            return appObject.create(on: conn).map { appObject in
                try ipaFile.save(with: payload.ipaFile.data)
                try plistFile.save(with: payload.plistFile.data)
                try imageFile.save(with: payload.imageFile.data)
            }
        }
    }
}

ServerFile结构

struct ServerFile {
    enum Folder: String {
        case ipa = "ipa"
        case plist = "plists"
        case image = "images"
        case root = ""
    }

    let file, ext: String
    let folder: Folder

    init (file: String? = UUID().uuidString, ext: String, folder: Folder? = .root) {
        self.file = file
        self.ext = ext
        self.folder = folder
    }

    var relativePath: String {
        guard folder != .root else { return fileWithExt }
        return folder.rawValue + "/" + fileWithExt
    }

    var fileWithExt: String { return file + "." + ext }

    func save(with data: Data) throws {
        /// Get path to project's dir
        let workDir = DirectoryConfig.detect().workDir
        /// Build path to Public folder
        let publicDir = workDir.appending("Public")
        /// Build path to file folder
        let fileFolder = publicDir + "/" + folder.rawValue
        /// Create file folder if needed
        var isDir : ObjCBool = true
        if !FileManager.default.fileExists(atPath: fileFolder, isDirectory: &isDir) {
            try FileManager.default.createDirectory(atPath: fileFolder, withIntermediateDirectories: true)
        }
        let filePath = publicDir + "/" + relativePath
        /// Save data into file
        try data.write(to: URL(fileURLWithPath: filePath))
    }
}

声明对象模型

struct AppObject: Codable {
    var id: Int
    var ipaFile, plistFile, imageFile: String
    var name, notes: String
}

使用CodyFire库,多部分请求非常容易

声明您的终结点

import CodyFire

struct AppController: EndpointController {
    static var server: ServerURL? = nil
    static var endpoint: String = "apps"
}

/// Usually separate file like App+Create.swift
extension AppController {
    struct CreateAppRequest: MultipartPayload {
        var ipaFile, plistFile, imageFile: Attachment
        var name, note: String
        public init (ipaFile: Attachment, plistFile: Attachment, imageFile: Attachment, name: String, note: String) {
            self.ipaFile = ipaFile
            self.plistFile = plistFile
            self.imageFile = imageFile
            self.name = name
            self.note = note
        }
    }

    static func create(_ payload: CreateAppRequest) -> APIRequest<AppObject> {
        return request(payload: payload).method(.post)
    }
}

然后,在某些视图控制器中,尝试在服务器上创建应用程序

/// Replace _ with file data
let ipaFile = Attachment(data: _, fileName: "", mimeType: "ipa")
let plistFile = Attachment(data: _, fileName: "", mimeType: "plist")
let imageFile = Attachment(data: _, fileName: "", mimeType: .jpg)

let payload = AppController.CreateAppRequest(ipaFile: ipaFile, 
                                             plistFile: plistFile,
                                             imageFile: imageFile,
                                             name: "something", 
                                             note: "something")

AppController.create(payload).onRequestStarted {
    /// it calls only if request started properly
    /// start showing loading bar
}.onError { error in
    let alert = UIAlertController(title: nil, message: error.description, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .cancel))
    self.present(alert, animated: true)
}.onProgress { progress in
    /// show progress
}.onSuccess { appObject in
    /// show success
    /// here you received just created `appObject`
}

就这样,它只是起作用:)

获取AppObject列表的下一个示例

/// Separate file like App+List.swift
extension AppController {
    static func list() -> APIRequest<[AppObject]> {
        return request()
    }
}

然后在视图控制器的某处

AppController.list().onSuccess { appObjects in
    /// `appObjects` is `[AppObject]`
}

希望有帮助。

 类似资料:
  • 基本上,我正在尝试使用Web API 2上传图像和枚举。 这是控制器签名: 问题是,每当我尝试发布一个多部分表单(包含一个文件和一个类型)时,我都会收到一个415错误: {“Message”:“此资源不支持请求实体的媒体类型‘multipart/form data’。”,“ExceptionMessage”:“没有MediaTypeFormatter可用于从媒体类型为“multipart/form

  • 我已经设置了Spring Cloud Stream中提供的Spring Avro模式注册表,以便在RabbitMQ中使用。我看到的大多数示例都使用Maven Avro插件从模式资源文件生成Java类。然后在架构注册表中注册架构文件。我的理解是,此注册表允许消息仅通过对已注册架构的引用进行SERDE,而不是在消息中包含整个架构。我不明白的是,在设计时,这些模式文件是如何在所有服务之间分发的,以生成J

  • 我正在上传一个大的应用程序(大约250MB)。从家里,它在1个小时左右上传。 上载应用程序时出错。 服务器错误,状态代码:500,错误代码:0,消息: 有什么想法或建议吗?谢谢!

  • 问题内容: 我想为我的一个项目添加类似gmail的文件上传功能。有人可以帮我吗? 我的应用程序是在vb.net中构建的。 我将不胜感激任何帮助或指导。 谢谢 问题答案: 请查看SWFUpload,它实际上是Flash绝对优越的文件上传处理功能的JavaScript API。最好的东西,直到浏览器终于​​赶上。 来自链接: 通过对话框中的ctrl / shift-select一次上传多个文件 所有事

  • 问题内容: 假设我有一个模型Car,该模型在 ViewModel1中 实例化为以下初始属性: ViewModel1 然后,我需要在下一个视图控制器中完成汽车的其他信息。遵循MVVM时,在视图控制器之间传递模型的 正确 方法是什么? 使用MVC,操作很简单,因为视图可以引用模型: 以下是对该问题的伪尝试,但是我给人的印象是视图模型应该是私有的,并且只能由单个视图控制器访问。因此,以下尝试对我来说似乎

  • 这个问题已经出现过好几次了,尽管似乎没有任何有效的解释。(或者也许我没有在这个叫做互联网的混乱中找到它)。。。 我正在开发一个android应用程序,它会打开一个包含上传按钮的HTML页面。它在WebView中不起作用。 我试过:http://m0s-programming.blogspot.in/2011/02/file-upload-in-through-webview-on.html 但是e