Upload files

237 views
Skip to first unread message

alessio

unread,
Oct 25, 2011, 4:10:39 PM10/25/11
to clj-noir
Hello,

I am trying to use noir to upload a read a csv file. Would you guys
have some advises regarding this? I couldn't find any understandable
information for this task.

Thanks.

Chris Granger

unread,
Oct 25, 2011, 4:22:05 PM10/25/11
to clj-...@googlegroups.com
It's actually not much different from how you get params from any request. Assuming you have a file input called "myFile" it'd look something like this:

(defpage [:post "upload"] {:keys [myFile]}
  (println myFile) ;; see all the things the file contains
  (io/copy (io/file (:tempfile myFile)) (io/file "uploads/some-new-name")))

Cheers,
Chris.

Linus Ericsson

unread,
Oct 25, 2011, 4:29:06 PM10/25/11
to clj-...@googlegroups.com
The CSV could either be parsed by home-made parserfunction (not really recommended) or use any of the many availiable libs for this. Checkout 

https://github.com/davidsantiago/clojure-csv for a starter.

Just add the not-so-magic line for to project.clj and run lein deps, :use the namespace clojure-csv.core it in your code and send your incoming param-string there.

/Linus

2011/10/25 Chris Granger <ibd...@gmail.com>

clojure-newbie

unread,
Oct 25, 2011, 5:45:05 PM10/25/11
to clj-...@googlegroups.com
I found I got this error trying to upload with lein:

https://github.com/technomancy/leiningen/issues/262

Is there are workaround for this in noir ?

Chris Granger

unread,
Oct 25, 2011, 6:20:01 PM10/25/11
to clj-...@googlegroups.com
I've never had any issues with it, so I'm not sure. Can you post a gist of a small example that causes that for you?

Cheers,
Chris.

clojure-newbie

unread,
Oct 26, 2011, 3:32:15 AM10/26/11
to clj-...@googlegroups.com
Code here: https://gist.github.com/1315692

Error was as follows:

java.util.concurrent.RejectedExecutionException
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1956)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:816)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1337)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:120)
    at clojure.core$future_call.invoke(core.clj:6051)
    at ring.middleware.multipart_params.temp_file$temp_file_store$fn__107.invoke(temp_file.clj:47)
    at ring.middleware.multipart_params$parse_file_item.invoke(multipart_params.clj:48)
    at ring.middleware.multipart_params$parse_multipart_params$iter__490__494$fn__495.invoke(multipart_params.clj:56)
    at clojure.lang.LazySeq.sval(LazySeq.java:42)
    at clojure.lang.LazySeq.seq(LazySeq.java:60)
    at clojure.lang.RT.seq(RT.java:466)
    at clojure.core$seq.invoke(core.clj:133)
    at clojure.core$reduce.invoke(core.clj:5994)
    at clojure.core$into.invoke(core.clj:6004)
    at ring.middleware.multipart_params$parse_multipart_params.invoke(multipart_params.clj:54)
    at ring.middleware.multipart_params$wrap_multipart_params$fn__509.invoke(multipart_params.clj:97)
    at ring.middleware.session$wrap_session$fn__664.invoke(session.clj:40)
    at ring.middleware.cookies$wrap_cookies$fn__601.invoke(cookies.clj:132)
    at noir.session$noir_session$fn__2211.invoke(session.clj:52)
    at ring.middleware.session$wrap_session$fn__664.invoke(session.clj:40)
    at ring.middleware.cookies$wrap_cookies$fn__601.invoke(cookies.clj:132)
    at noir.cookies$noir_cookies$fn__2065.invoke(cookies.clj:66)
    at ring.middleware.cookies$wrap_cookies$fn__601.invoke(cookies.clj:132)
    at noir.validation$wrap_noir_validation$fn__2238.invoke(validation.clj:88)
    at noir.statuses$wrap_status_pages$fn__2160.invoke(statuses.clj:23)
    at ring.middleware.reload_modified$wrap_reload_modified$fn__801.invoke(reload_modified.clj:15)
    at noir.server.handler$wrap_url_decode$fn__2247.invoke(handler.clj:26)
    at noir.exception$wrap_exceptions$fn__2191.invoke(exception.clj:59)
    at noir.options$wrap_options$fn__2154.invoke(options.clj:31)
    at ring.adapter.jetty$proxy_handler$fn__733.invoke(jetty.clj:16)
    at ring.adapter.jetty.proxy$org.mortbay.jetty.handler.AbstractHandler$0.handle(Unknown Source)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

Alessio Civitillo

unread,
Oct 26, 2011, 6:53:45 AM10/26/11
to clj-noir
Up.

Somebody that could help with this?
--
Regards,
------------------------------------
Alessio Civitillo
alessioc...@gmail.com
Mobile: (0045) 52645608
Linkedin: http://it.linkedin.com/in/alessiocivitillo

Chris Granger

unread,
Oct 26, 2011, 1:03:35 PM10/26/11
to clj-...@googlegroups.com
clojure-newbie, I asked technomancy about this and he said that leiningen 1.6.1.1 was basically put out solely to fix that issue. So if you upgrade your leiningen, you should be good to go. :)

Cheers,
Chris.

Chris Granger

unread,
Oct 26, 2011, 1:06:01 PM10/26/11
to clj-...@googlegroups.com
Alessio,

We've answered all the parts of your question already. You use defpage to create a route for your file upload. You then slurp the tempfile and run it through a csv parser:

(defpage [:post "upload"] {:keys [myFile]}
  (println myFile) ;; see all the things the file contains
  (let [contents (slurp (io/file (:tempfile myFile)))]
     (parse-csv contents)))

Cheers,
Chris.

Dan Jacob

unread,
Oct 26, 2011, 1:16:28 PM10/26/11
to clj-...@googlegroups.com

Thanks for this !


alessio

unread,
Oct 26, 2011, 4:31:51 PM10/26/11
to clj-noir
Hello,

sorry for the 'up'. I receive a digest and I did not notice your
previous email until too late.

Your help has been great, problem solved!
Reply all
Reply to author
Forward
0 new messages