On Tue, Dec 1, 2009 at 8:03 PM, DMB <[email protected]> wrote:
> Ended up doing this:
>
> def show(xhtml: NodeSeq) : NodeSeq = {
> if(S.post_?) {
> val f : Option[FileParamHolder] = S.request match {
> case Empty => Empty
> case Full(req) =>
> req.uploadedFiles.find(_.name == "file_upload")
> }
>
> if(f.isDefined) {
> acceptFile(f.get)
> }
> }
> return xhtml
> }
>
This is a place for a for comprehension:
def show(xhtml: NodeSeq): NodeSeq = {
for {
request <- S.request if S.post_?
file <- request.uploadedFiles.find(_.name == "file_upload")
} acceptFile(f.get)
xhtml
}
Note that the code is shorter, reads better and has no branches in it.
>
> And putting a bare upload field in the snippet, like you sugested. I
> still wonder if the binding method is the best place for this, though.
>
> On Dec 1, 10:14 am, David Pollak <[email protected]>
> wrote:
> > If you have the Req (request), you can do:
> >
> > val req: Req = ...
> > val theFile: Option[FileParamHolder] = req.uploadedFiles.find(_.name ==
> > "my_param_name")
> >
> >
> >
> > On Tue, Dec 1, 2009 at 2:49 AM, DMB <[email protected]> wrote:
> > > Just what I was looking for, thanks!
> >
> > > By the way, is there a way to assign a "name" attribute to a file
> > > upload input field? The reason is I have a legacy piece of code that
> > > does HTTP uploads by simulating a form submit. This code requires that
> > > the form field name be known in advance, for obvious reasons.
> >
> > > On Nov 30, 9:26 pm, David Pollak <[email protected]>
> > > wrote:
> > > > Folks,
> >
> > > > Lately there's been a bunch of chatter on the list about image upload
> and
> > > > figuring out how to put the image files in the right place to serve
> them
> > > > again.
> >
> > > > I've written a simple example of image uploading, storing the image
> in
> > > the
> > > > RDBMS and then serving the image. You can find the code at:
> > >http://github.com/dpp/imagine/
> >
> > > > The file upload snippet is very simple:
> >
> > > > class DoUpload {
> > > > private def saveFile(fp: FileParamHolder): Unit = {
> > > > fp.file match {
> > > > case null =>
> > > > case x if x.length == 0 =>
> > > > case x =>
> > > > val blob = ImageBlob.create.image(x).saveMe
> > > > ImageInfo.create.name
> > > > (fp.fileName).mimeType(fp.mimeType).blob(blob).saveMe
> > > > S.notice("Thanks for the upload")
> > > > S.redirectTo("/")
> > > > }
> > > > }
> >
> > > > def render(in: NodeSeq): NodeSeq =
> > > > bind("upload", in, "file" -> SHtml.fileUpload(saveFile _))
> >
> > > > }
> >
> > > > If the blob is uploaded, it's put in a row in ImageBlob and an
> ImageInfo
> > > row
> > > > is created to store the name and mime type. There are two different
> rows
> > > so
> > > > that one doesn't have to pull the whole blob back when you're
> checking
> > > for
> > > > the upload date.
> >
> > > > Serving the image is similarly concise:
> >
> > > > def serveImage: LiftRules.DispatchPF = {
> > > > case req @ Req("images" :: _ :: Nil, _, GetRequest) if
> > > > findFromRequest(req).isDefined =>
> > > > () => {
> > > > val info = findFromRequest(req).open_! // open is valid here
> > > because
> > > > we just tested in the guard
> >
> > > > // Test for expiration
> > > > req.testFor304(info.date, "Expires" -> toInternetDate(millis
> +
> > > > 30.days)) or
> > > > // load the blob and return it
> > > > info.blob.obj.map(blob => InMemoryResponse(blob.image,
> > > > List(("Last-Modified", toInternetDate(info.date.is)),
> >
> > > > ("Expires", toInternetDate(millis + 30.days)),
> >
> > > > ("Content-Type", info.mimeType.is)), Nil, 200))
> > > > }
> > > > }
> >
> > > > If the request matches /images/xxx where xxx is a name in the
> ImageInfo
> > > > table, check to see if we can return a 304 (not modified). If not,
> we
> > > pull
> > > > the blob from the database and return it.
> >
> > > > I hope this helps folks who are looking to manage and serve images.
> >
> > > > Thanks,
> >
> > > > David
> >
> > > > --
> > > > Lift, the simply functional web frameworkhttp://liftweb.net
> > > > Beginning Scalahttp://www.apress.com/book/view/1430219890
> > > > Follow me:http://twitter.com/dpp
> > > > Surf the harmonics
> >
> > > --
> >
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Lift" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<liftweb%[email protected]>
> <liftweb%[email protected]<liftweb%[email protected]>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/liftweb?hl=en.
> >
> > --
> > Lift, the simply functional web frameworkhttp://liftweb.net
> > Beginning Scalahttp://www.apress.com/book/view/1430219890
> > Follow me:http://twitter.com/dpp
> > Surf the harmonics
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<liftweb%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>
>
>
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
--
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en.