ixx
ixx

Reputation: 32273

Lift file uploader compiler error

I want to implement an image uplad with Lift. Started with this. I'm new to Lift.

I made some modifications. For now I just want to see something working. Not necessarily the images. So far I have:

package code 
package snippet 
import net.liftweb.http.S
import net.liftweb.common.Full
import net.liftweb.common.Empty
import net.liftweb.common.Box
import net.liftweb.http.FileParamHolder
import net.liftweb.util._
import Helpers._
import scala.xml.Group
import scala.xml.NodeSeq
import net.liftweb.http.SHtml

class AddEntry {

  // Add a variable to hold the FileParamHolder on submission
  var fileHolder : Box[FileParamHolder] = Empty

  def doTagsAndSubmit (t : String) {

//    val e : Expense = ...
    // Add the optional receipt if it’s the correct type
    val receiptOk = fileHolder match {
      // An empty upload gets reported with a null mime type,
      // so we need to handle this special case
      case Full(FileParamHolder(_, null, _, _)) => true

      case Full(FileParamHolder(_, mime, _, data))
        if mime.startsWith("image/") => {
//          e.receipt(data).receiptMime(mime)
          true
        }

      case Full(_) => {
        S.error("Invalid receipt attachment")
        false
      }

      case _ => true
    }

//    (e.validate, receiptOk) match {
//    }
}

  def addEntry(content: NodeSeq): NodeSeq = {

      bind("prefix", content,
              "receipt" -> SHtml.fileUpload(fileHolder = _)) //compiler error: type mismatch; found : net.liftweb.http.FileParamHolder required: net.liftweb.common.Box[net.liftweb.http.FileParamHolder]
  }
}

The html:

<lift:AddEntry.addEntry form="POST" multipart="true">
    <td>Receipt (JPEG or PNG)</td>
<td><prefix:receipt /></td>
</lift:AddEntry.addEntry>

As commented in the code I get in "receipt" -> SHtml.fileUpload(fileHolder = _) a compiler error: error: type mismatch; found : net.liftweb.http.FileParamHolder required: net.liftweb.common.Box[net.liftweb.http.FileParamHolder]

Don't understand it, because fileHolder is of type Box[FileParamHolder]. It should be related with the expression fileHolder = _, but don't know how.

Thanks in advance.

Upvotes: 1

Views: 181

Answers (1)

jcern
jcern

Reputation: 7848

From the API it seems that SHtml.fileUpload provides a FileParamHolder but your variable fileHolder is of type Box[FileParamHolder], so setting those two does not work out of the box.

If you try this:

SHtml.fileUpload(f => fileHolder = Box !! f))

It will provide you with a Box[FileParamHolder] and that should allow you to compile your code.

Upvotes: 3

Related Questions