Comments (3)
hi @swierpa :) please have a look at the sample code below:
class ViewController: UIViewController {
private var server: HttpServer?
override func viewDidLoad() {
super.viewDidLoad()
let server = HttpServer();
server.get["/upload"] = { r in
return .OK(.Html("<form method=\"POST\" action=\"/upload\" enctype=\"multipart/form-data\">" +
"<input name=\"my_file\" type=\"file\"/>" +
"<button type=\"submit\">Send File</button>" +
"</form>"))
}
server.post["/upload"] = { r in
if let myFileMultipart = r.parseMultiPartFormData().filter({ $0.name == "my_file" }).first {
guard let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first else {
return .InternalServerError
}
let data: NSData = myFileMultipart.body.withUnsafeBufferPointer { pointer in
return NSData(bytes: pointer.baseAddress, length: myFileMultipart.body.count)
}
guard let fileSaveUrl = NSURL(string: "name_for_file.txt", relativeToURL: documentsUrl) else {
return .InternalServerError
}
print(fileSaveUrl)
data.writeToURL(fileSaveUrl, atomically: true)
return .OK(.Html("Your file has been uploaded !"))
}
return .InternalServerError
}
do {
try server.start(9099)
} catch {
print("Server start error: \(error)")
}
self.server = server
}
}
best,
dk
from swifter.
Hi @glock45
Thank you for the solution its help a lot to me. but your solution is outdated because of the swift update.
so I did some Changes in your solution. Please verify that.
server.post["/upload"] = { r in
if let multipart = r.parseMultiPartFormData().filter({ $0.name == "newfile" }).first {
guard let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return .internalServerError
}
let data: NSData = multipart.body.withUnsafeBufferPointer { pointer in
return NSData(bytes: pointer.baseAddress, length: multipart.body.count)
}
guard let fileSaveUrl = NSURL(string: multipart.fileName ?? "NoNameFile.txt", relativeTo: documentsUrl) else {
return .internalServerError
}
print(fileSaveUrl)
data.write(to: fileSaveUrl as URL, atomically: true)
return HttpResponse.ok(.html("Your file has been uploaded !"))
}
return .internalServerError
}
Thank you,
Anup Gupta
from swifter.
How to Upload Folder ???
I am able to upload a single file successfully.
selection of the folder and and in server.post["/upload"]
I am geeting all files of folder.
but unable to upload folder.
from swifter.
Related Issues (20)
- How can I call an async function with callback from within the route callback and still be able to return a HttpResponse
- websocket sometimes not working in macOS sandbox app HOT 1
- LICENSE file contains placeholder text.
- usleep degrades when using Swifter
- Unable to connect to server HOT 1
- tvOS 16 Beta 2 Unable to override the Stub that has same url
- Builds fails when trying to use `Swifter` in XCUITest HOT 3
- 404 Not Found HOT 1
- Building release is extremely slow
- Enhancement request: Swifter to label .wasm files as "application/wasm" when it serves them
- Can the development on this package be sponsored?
- IOS 16.4problem: Restricted network ports are not allowed HOT 3
- How to get post parameters ?
- How to set multiple responses for one endpoint?
- Question for loading index.html + subdirrectories HOT 1
- Support regular expression
- Bind operation not permitted - MacOS - UITests
- Privacy Manifest HOT 6
- HttpServer does not support for multicast
- Release new version from stable branch (Cocoapods)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swifter.