Perfect 文件上传下载

func resignRoutes() {
    // 文件上传
    routes.add(method: .post, uri: "/upload") { (req, resp) in
        
        // 通过操作fileUploads数组来掌握文件上传的情况
        // 如果这个POST请求不是分段multi-part类型,则该数组内容为空
        if let uploads = req.postFileUploads , uploads.count > 0 {
            // 创建文件保存路径
            let fileDir = Dir(Dir.workingDir.path + "files")
            do {
                try fileDir.create()
            } catch {
                resp.appendBody(string: "create failed")
                print(error)
            }
            
            // 创建一个字典数组用于检查已经上载的内容
            var ary = [[String:Any]]()
            
            for upload in uploads {
                ary.append([
                    "fieldName": upload.fieldName,  //字段名
                    "contentType": upload.contentType, //文件内容类型
                    "fileName": upload.fileName,    //文件名
                    "fileSize": upload.fileSize,    //文件尺寸
                    "tmpFileName": upload.tmpFileName   //上载后的临时文件名
                    ])
                
                // 获取上传的临时文件
                let thisFile = File(upload.tmpFileName)
                
                do {
                    let _ = try thisFile.moveTo(path: fileDir.path + upload.fileName, overWrite: true)
                } catch {
                    resp.appendBody(string: "remove failed")
                    print(error)
                }
                
            }
            
            
            resp.appendBody(string: "upload success")
            
            
        } else {
            resp.appendBody(string: "upload fail")
            
        }
        resp.completed()
    }
    
    
    routes.add(method: .get, uri: "/download/**") { (req, resp) in
        req.path = req.urlVariables[routeTrailingWildcardKey]!
        let handler = StaticFileHandler(documentRoot: Dir.workingDir.path + "files")
        
        handler.handleRequest(request: req, response: resp)
    }
    server.addRoutes(routes)

}

你可能感兴趣的:(Perfect 文件上传下载)