Fileupload

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Fileupload

getagrip
Hi,

how can I post a File using Dispatch?
Using the following post-data it does not work:

val post = server << Map(
  "id" -> "42",
  "myfile" -> new File(path/to/file)
)
Reply | Threaded
Open this post in threaded view
|

Re: Fileupload

n8han
Administrator
You need to depend on the "dispatch-mime" module which adds multipart post support, then "import Mime._" and you'll have a new << for File objects on request:

http://sourced.implicit.ly/net.databinder/dispatch-mime/0.7.7/dispatch/Mime.scala.html#10631

Nathan

On 11/1/10 2:08 PM, getagrip [via Databinder] wrote:
Hi,

how can I post a File using Dispatch?
Using the following post-data it does not work:

val post = server << Map(
  "id" -> "42",
  "myfile" -> new File(path/to/file)
)



View message @ http://databinder.3617998.n2.nabble.com/Fileupload-tp5694580p5694580.html
To start a new topic under Databinder, email [hidden email]
To unsubscribe from Databinder, click here.


Reply | Threaded
Open this post in threaded view
|

Re: Fileupload

getagrip
Thanks for the fast reply!

Could you provide a short example how to use it as Scala is still a bit weird for a Java dev like me?

I guess that the << method expects a function which returns a Stream so if I have a ByteArrayOutputStream I should provide a function that returns it, something like this maybe:

val multipart =  new MimeRequest().<<("name", url, getStreamFunc, "application/octet-stream")

This yields a "too many arguments for method <<" error though.

val post = server << Map(
  "id" -> "42",
  "myfile" -> multipart
)

http(post / command >>> System.out)

Reply | Threaded
Open this post in threaded view
|

Re: Fileupload

n8han
Administrator
On 11/02/2010 06:58 AM, getagrip [via Databinder] wrote:
> Could you provide a short example how to use it as Scala is still a
> bit weird for a Java dev like me?

Sure:

import Mime._
val post = server << Map(
   "id" -> "42"
) << ("myfile", file)

That is the way to do it if you have a java.io.File.

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: Fileupload

getagrip
Thanks for the hint, I still have trouble using the Mime's "<<" method.
In the example the first "<<" method is of type Request, the second "<<" method of class MimeRequest but the compiler does not see it:

overloaded method value << with alternatives: (string_body: String)dispatch.Request <and> (values: scala.collection.Map[String,Any])dispatch.Request cannot be applied to (java.lang.String, java.io.File)

val post = :/("localhost") << Map(
  "commit" -> "true",
  ) << ("filename", new java.io.File("path/to/file"))

val http = new dispatch.Http
http(post >>> System.out)
Reply | Threaded
Open this post in threaded view
|

Re: Fileupload

getagrip
Ah, I had to use both of these imports:

import dispatch.mime._
import Mime._

Thanks again for the help!
Reply | Threaded
Open this post in threaded view
|

Re: Fileupload

n8han
Administrator
On 11/2/10 10:10 AM, getagrip [via Databinder] wrote:
> Ah, I had to use both of these imports:
>
> import dispatch.mime._
> import Mime._
>
> Thanks again for the help!

No problem. There is some explanation of extension by implicit
conversion in this post, if you're curious, under the "Carte bl@nche"
section:

http://technically.us/code/x/oauth-here-she-comes/

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: Fileupload

getagrip
Thanks for the link, very interesting read.

Could you provide a small example in case I did not want to use a file but a Stream instead?
It looks like the << method expects a function returning an InputStream but the following somehow does not work:

def getStream = new  FileInputStream(new File("/path/to/file"))

val post = server << Map(
  "id" -> "42",
) << ("name","filename", getStream)
Reply | Threaded
Open this post in threaded view
|

Re: Fileupload

getagrip
What's weird is that the implicit conversion mechanism works with Files but not with Streams.
It throws the same exception as above.