简体   繁体   中英

Scala liftweb framework

I have two checkboxes and on submit I want the user to download two different files, how do I go about this? I need help as am not able to get any online material on this. I am new to this framework.

This is what I've tried so far

def render = {
 def onSubmit1() : LiftResponse = {
       val userInput1 = S.param("getit").openOr("")
      val userInput2 = S.param("getit2").openOr("")
      val checkbox1 = S.param("yes").openOr("")
      val checkbox2  = S.param("yes").openOr("")
val fileToDownload = funFile(as, art, top)
       val fileName = "My File" 
 if (fileToDownload.equals()) {
         return S.redirectTo("/Somewhere")
       } else {
         InMemoryResponse(
          fileToDownload.mkString("\n").getBytes("UTF-8"),
            "Content-Type" -> "text/plain; charset=utf8" ::
             "Content-Disposition" -> s"attachment; filename=$fileName" :: Nil,
           cookies = Nil, code = 200)
       }
"#submitButton" #> SHtml.onSubmitUnit(onSubmit1)

I found a way to do that and that is to zip the two files using this function How do I archive multiple files into a .zip file using scala? ,

if (checkbox1.getOrElse("") == "checked" && checkbox2.getOrElse("") == "checked") {
val checkboxIterable1: Iterable[String] = filegetter1
        val checkboxFile1 = new File("checkboxFile1.csv")
        using(new FileWriter(checkboxFile1))(writer =>
          fileGetter1.foreach { d =>
            writer.write(d)
          })
       fileGetter1

        val checkboxIterable2: Iterable[String] = fileGetter2
        val checkboxFile2 = new File("checkboxFile2.csv")
        using(new FileWriter(checkboxFile2))(writer =>
          fileGetter2.foreach { d =>
            writer.write(d)
          })
        fileGetter2

        val zipFile = zip("Zipped file", List("checkboxFile1.csv", "checkboxFile2.csv"))
        val zipFileName = "Zipped file"

        val zipToBArray = new BufferedInputStream(new FileInputStream(zipFileName))
        val getByteArray = Stream.continually(zipToBArray.read).takeWhile(-1 !=).map(_.toByte).toArray

        InMemoryResponse(
          getByteArray,
          "Content-Type" -> "application/zip; charset=utf8" ::
            "Content-Disposition" -> s"attachment; filename=file.zip" :: Nil,
          cookies = Nil, code = 200)
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM