Upload file from website

There is a code that accepts files through a multipart/form-data type website webform, written using Perfect. The following function processes the result:
import Foundation
import PerfectLib
import PerfectHTTP
import PerfectMustache

guard var path = request.param(name: "path") else {
    httpErrorResponse(response: response, e: CMSError.BadRequest)
    return
}

path = webroot + "/" + path + "/"
if let uploads = request.postFileUploads, uploads.count > 0 {

    var array = [FileNode]()
    
    for upload in uploads {
        array.append(FileNode(name: upload.fileName, type: "file"))
        
        print("Uploading to path: \(path), filename: \(upload.fileName), tmpname: \(upload.tmpFileName)")
        // move file to webroot
        let thisFile = File(upload.tmpFileName)
        do {
            let newFile = try thisFile.moveTo(path: path + upload.fileName, overWrite: true)
            newFile.perms = [.rwxUser, .rxGroup, .rxOther]
        } catch {
            print(error)
        }
        
    }
}

struct FileNode: Encodable {
	let name: String
	let type: String
}

In Vapor, how can I get a list of files?

Multipart works in the same way as any other type with Content in Vapor, though you can do more advanced things if you want to stream the upload to a file etc. See File upload using Vapor 4 - The.Swift.Dev. as a good starter

Terms of Service

Privacy Policy

Cookie Policy