0

I have never worked on any kind of the server side language before. I am starting to learn it with swift. So my apologies in advance if i ask something silly.

so here is the story

I want to upload a file to

<path to my project>/Resources/fileuplaods

i was following Perfect 2.0 documentation, Here is my code :-

routes.add(method: .post, uri: "/webroot/uploads") { (request, response) in

let fileDir = Dir("./Resources/fileuploads")
print("fileDir - \(fileDir)")
do {
    try fileDir.create()
}
catch {
    print(error)
}

if let uploads = request.postFileUploads , uploads.count > 0 {
    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 {
            print(error)
        }
    }
    print("ary - \(ary)")

}
response.setBody(string: "API Call uploads")
response.completed()
}

the following is my output :-

fileDir - Dir(internalPath: "./Resources/fileuploads/")
fileError(21, "Is a directory /Users/username/Documents/flone/fltwo/projname/Packages/PerfectLib-2.0.0/Sources/PerfectLib/File.swift moveTo(path:overWrite:) 286")
ary - [["contentType": "image/jpeg", "fileSize": 18587, "fileName": "", "fieldName": "", "tmpFileName": "/tmp/perfect_upload_rodB53"]]

What am i doing wrong ?

Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40

1 Answers1

2

Found the problem. actually i wan't adding filename in the destination directory.

had to replace

thisFile.moveTo(path: fileDir.path , overWrite: true)

with

thisFile.moveTo(path: fileDir.path + upload.fileName , overWrite: true)
Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40