Upload field on a lift screen

185 views
Skip to first unread message

Tareq Abedrabbo

unread,
Oct 18, 2011, 7:19:55 AM10/18/11
to Lift
Hi all,

I'm wondering how to create a file upload field on a Lift Screen. I
can see that Lift Screen checks for upload fields and that Field has
an uploadField_? property but I'm not clear on whether a call to the
field method can render a file upload field and what the default value
would be in this case.
Thanks in advance for your help.

Tareq

David Pollak

unread,
Oct 18, 2011, 2:38:52 PM10/18/11
to lif...@googlegroups.com
You have to create a custom field.  Here's a Screen that does an upload:

import net.liftweb._
import http._
import common._
import scala.xml._

class UploadScreen extends LiftScreen {
  override protected def hasUploadField = true

  val name = field("Name", "")
  val file = makeField[Array[Byte], Nothing]("File", new Array[Byte](0),
    field => SHtml.fileUpload(fph => field.set(fph.file)),
    NothingOtherValueInitializer)

  def finish() {
    S.notice("Thanks for uploading a file of " + file.get.length + " bytes ")
  }
}



--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Lift, the simply functional web framework http://liftweb.net

Readman

unread,
Sep 18, 2012, 1:27:25 PM9/18/12
to lif...@googlegroups.com
Hey David,
Thanks for posting this example.
However, what is the upload directory in this example? I can not see any file in folder, when i uploaded a file

Chenguang He

Olek Swirski

unread,
Sep 18, 2012, 3:05:41 PM9/18/12
to lif...@googlegroups.com
file goes to whatever is temp dir for your app,
which may quite possibly be /tmp
in that case, file may quickly be cleaned by OS,
so then you would need to keep reference
to FileParamHolder and move it elsewhere or
maybe setting tmp dir for your app to different
value with System.setProperty or by giving
command line arg to java
--

Olek Swirski

unread,
Sep 18, 2012, 3:11:24 PM9/18/12
to lif...@googlegroups.com
oh, I forgot. by default file will be loaded into the memory
and never appear on the hard drive. then fph is your
only handle to do something with it before it disappears.

to get on disk file you need to call this in your boot:
    /* store uploads as files on disk */
    LiftRules.handleMimeFile = OnDiskFileParamHolder.apply
you may also need these:
    /* set max total upload size */
    LiftRules.maxMimeSize = 1024 * 1024 * 32
    /* set max per-file upload size */
    LiftRules.maxMimeFileSize = 1024 * 1024 * 32;

also, that file will have some random name, because
otherwise there could easily be name clash if someone
uploads two same named files to your application. you
may restore original upload name from fph.fileName

Chenguang He

unread,
Sep 18, 2012, 4:16:57 PM9/18/12
to lif...@googlegroups.com
Uhm..
Sorry, i still don't know how to upload file to a  specific folder.
Does fph store the file or the location in memory?
   
Chenguang He



Olek Swirski

unread,
Sep 18, 2012, 4:24:42 PM9/18/12
to lif...@googlegroups.com
If you set up
LiftRules.handleMimeFile = OnDiskFileParamHolder.apply
then, the the file will be accessible as fph.localFile
if you use InMemFileParamHolder (which I think is default)
then, you get file bytes via
val
file: Array[Byte]
and need to write them yourself somewhere, which can be
done with java.io or java.nio (you may need to read javadoc
for these, which are very comprehensive)

Olek Swirski

unread,
Sep 18, 2012, 4:31:49 PM9/18/12
to lif...@googlegroups.com

Chenguang He

unread,
Sep 18, 2012, 6:51:08 PM9/18/12
to lif...@googlegroups.com
Thank you.
It works!
Chenguang He



Reply all
Reply to author
Forward
0 new messages