我想用Lift实现图像uplad。从this开始。我是Lift的新手。
我做了一些修改。现在我只想看到一些有用的东西。不一定是图像。到目前为止,我有:
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]
}
}
html:
<lift:AddEntry.addEntry form="POST" multipart="true">
<td>Receipt (JPEG or PNG)</td>
<td><prefix:receipt /></td>
</lift:AddEntry.addEntry>
在代码中注释我在"receipt" -> SHtml.fileUpload(fileHolder = _)
中遇到编译器错误:错误:类型不匹配;发现:net.liftweb.http.FileParamHolder required:net.liftweb.common.Box [net.liftweb.http.FileParamHolder]
不明白,因为fileHolder的类型为Box[FileParamHolder]
。它应该与表达式fileHolder = _
相关,但不知道如何。
提前致谢。
答案 0 :(得分:3)
从API看来,SHtml.fileUpload
似乎提供了FileParamHolder
,但您的变量fileHolder
的类型为Box[FileParamHolder]
,因此将这两者设置为开箱即用。
如果您尝试这样做:
SHtml.fileUpload(f => fileHolder = Box !! f))
它将为您提供Box[FileParamHolder]
,并允许您编译代码。