0

I'm trying out @tototoshi's scala-csv library and its very simple, looks like this:

def downloadFile = Action {
    val f = new File("out.csv")
    val writer = CSVWriter.open(f)
    writer.writeAll(List(List("a", "b", "c"), List("d", "e", "f")))
    writer.close()
    Ok.sendFile(f, inline = false, _ => f.getName)
  }

but now I the file is getting download to my project directory:

enter image description here

and I want it to get downloaded to the default download folder of whoever use this func, how can I do this?

erip
  • 16,374
  • 11
  • 66
  • 121
JohnBigs
  • 2,691
  • 3
  • 31
  • 61

2 Answers2

1

the file is getting download to my project directory

This has nothing to do with Scala or with scala-csv, it's just the way new File(String) constructor works: if you pass a relative path like "out.csv" (or more generally, "directory/directory/..."), it uses the working directory, which is set by the "run configuration" when running in IDEA. Use an absolute path ("C:/directory/..." on Windows or "/directory/..." on Linux/Mac) instead.

to the default download folder

There is no built-in way to find default download folder in Java, it depends on the OS. See General Path To Downloads Folder for an answer for Windows.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
0

You'll need to resolve the download folder yourself. The library will, given a path, write a CSV to that path. It'll include other CSV facilities, but definitely not path resolving code.

To the best of my knowledge, there's no one environment variable that will point to default "Downloads" directories...

If you're on *nix, typically downloads are in ~/Downloads, so your path will be "~/Downloads".replace("~", System.getProperty("user.home")). This can then be passed into java.io.File's constructor.

I don't know Windows' filesystem well enough to tell you if this will work for Windows as well, but a cursory search suggests it should work, too.

erip
  • 16,374
  • 11
  • 66
  • 121