crockpotveggies
crockpotveggies

Reputation: 13320

How to convert Play! 2.0 Scala file into a format for CSVReader?

I'm having some issues getting file uploads to work correctly and the following code does not compile.

The error: overloaded method constructor File with alternatives: (java.net.URI)java.io.File <and> (java.lang.String)java.io.File cannot be applied to (play.api.mvc.MultipartFormData.FilePart[play.api.libs.Files.TemporaryFile])

The code:

def csvimport = Action(parse.multipartFormData) { request =>
      request.body.file("files[]").map { rawfile =>

        val filename = rawfile.filename 
        val contentType = rawfile.contentType
        val tmpFile = new File(rawfile)

        val reader = new CSVReader(new FileReader(tmpFile))

        Ok
      }
      .getOrElse {
        Logger.debug(request.body.toString)
        BadRequest
      }
    }

Do I need to temporarily store the file to disk before I run a CSVReader on it? Is there not a way to convert a raw file upload to a new File class in memory?

Thanks!

Upvotes: 3

Views: 1138

Answers (2)

Vincil Bishop
Vincil Bishop

Reputation: 1624

Had the same problem:

Try:

request.body.file("Image").ref.file // Java.io.File

request.body is play.api.mvc.MultipartFormData

request.body.file("Image").ref is play.api.libs.Files.TemporaryFile here: https://www.playframework.com/documentation/2.0/api/scala/play/api/libs/Files$$TemporaryFile.html

Upvotes: 0

Sadache
Sadache

Reputation: 651

Here is I guess what you are trying to achieve

https://stackoverflow.com/a/11059295/317452

https://gist.github.com/2939230

This parses progressively chunks of the file without putting the whole raw file in memory or on disk.

Upvotes: 1

Related Questions