Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Play upload file within a form

How would I upload a file within a form defined with Scala Play's play.api.data.Forms framework. I want the file to be stored under Treatment Image.

  val cForm: Form[NewComplication] = Form(    
mapping(    
  "Name of Vital Sign:" -> of(Formats.longFormat),    
  "Complication Name:" -> text,    
  "Definition:" -> text,    
  "Reason:" -> text,    
  "Treatment:" -> text,    
  "Treatment Image:" -> /*THIS IS WHERE I WANT THE FILE*/,                
  "Notes:" -> text,    
  "Weblinks:" -> text,    
  "Upper or Lower Bound:" -> text)    
  (NewComplication.apply _ )(NewComplication.unapply _ ))  

is there a simple way to do this? By using built in Formats?

like image 960
Lilluda 5 Avatar asked Jun 01 '14 18:06

Lilluda 5


People also ask

How do I upload a file to Scala?

Uploading files in a form using multipart/form-data The standard way to upload files in a web application is to use a form with a special multipart/form-data encoding, which lets you mix standard form data with file attachment data. Note: The HTTP method used to submit the form must be POST (not GET ).

What is spring boot MultipartFile?

public interface MultipartFile extends InputStreamSource. A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired ...

What is multipart file upload?

What is Multipart Upload Request? ​ A multipart request is a HTTP request that HTTP clients construct to send files and data over to a HTTP Server. It is commonly used by browsers and HTTP clients to upload files to the server.

How do I upload files to playwright?

To upload a file using Playwright use setInputFiles(selector, files[, options]) function. This method takes the selector of the input element and the path to the file you want to upload. The files parameter value can be a relative path (relative to the current working directory) or an absolute path.


1 Answers

I think you have to handle the file component of a multipart upload separately and combine it with your form data afterwards. You could do this several ways, depending on what you want the treatment image field to actually be (the file-path as a String, or, to take you literally, as a java.io.File object.)

For that last option, you could make the treatment image field of your NewComplication case class an Option[java.io.File] and handle it in your form mapping with ignored(Option.empty[java.io.File]) (so it won't be bound with the other data.) Then in your action do something like this:

def createPost = Action(parse.multipartFormData) { implicit request =>
  request.body.file("treatment_image").map { picture =>
    // retrieve the image and put it where you want...
    val imageFile = new java.io.File("myFileName")
    picture.ref.moveTo(imageFile)

    // handle the other form data
    cForm.bindFromRequest.fold(
      errForm => BadRequest("Ooops"),

      complication => {
        // Combine the file and form data...
        val withPicture = complication.copy(image = Some(imageFile))

        // Do something with result...

        Redirect("/whereever").flashing("success" -> "hooray")
      }
    )
  }.getOrElse(BadRequest("Missing picture."))
}

A similar thing would apply if you wanted just to store the file path.

There are several ways to handle file upload which will usually depend on what you're doing with the files server-side, so I think this approach makes sense.

like image 124
Mikesname Avatar answered Oct 06 '22 20:10

Mikesname