Skip to content

scalajs-io/multer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multer API for Scala.js

multer - Middleware for handling multipart/form-data.

Description

Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files. It is written on top of busboy for maximum efficiency.

NOTE: Multer will not process any form which is not multipart (multipart/form-data).

Build Dependencies

Build/publish the SDK locally

 $ sbt clean publish-local

Running the tests

Before running the tests the first time, you must ensure the npm packages are installed:

$ npm install

Then you can run the tests:

$ sbt test

Examples

Simple file upload
import io.scalajs.npm.express._
import io.scalajs.npm.express.multer._
import scalajs.js

val app = Express()
val upload = Multer(new MulterOptions(dest = "uploads/"))
app.post("/profile", upload.array(), (req: Request, res: Response, next: js.Function) => {
  // req.body contains the text fields
})
Multi-part file upload
import io.scalajs.npm.express._
import io.scalajs.npm.express.multer._
import scalajs.js

val upload = Multer(new MulterOptions(dest = "uploads/"))
val app = Express()
app.post("/profile", upload.single("avatar"), (req: Request, res: Response, next: js.Function) => {
    // req.file is the `avatar` file
    // req.body will hold the text fields, if there were any
})

app.post("/photos/upload", upload.array("photos", 12), (req: Request, res: Response, next: js.Function) => {
    // req.files is array of `photos` files
    // req.body will contain the text fields, if there were any
})

val cpUpload = upload.fields(js.Array(new MulterField(name = "avatar", maxCount = 1), new MulterField(name = "gallery", maxCount = 8)))
app.post("/cool-profile", cpUpload, (req: Request, res: Response, next: js.Function) => {
    // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
    //
    // e.g.
    //  req.files["avatar"][0] -> File
    //  req.files["gallery"] -> Array
    //
    // req.body will contain the text fields, if there were any
})

Artifacts and Resolvers

To add the Multer binding to your project, add the following to your build.sbt:

libraryDependencies += "io.scalajs.npm" %%% "multer" % "0.5.0"

Optionally, you may add the Sonatype Repository resolver:

resolvers += Resolver.sonatypeRepo("releases") 

Releases

No releases published

Packages

No packages published

Languages