Upload files

瀏覽次數:237 次
跳到第一則未讀訊息

alessio

未讀,
2011年10月25日 下午4:10:392011/10/25
收件者: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

未讀,
2011年10月25日 下午4:22:052011/10/25
收件者: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

未讀,
2011年10月25日 下午4:29:062011/10/25
收件者: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

未讀,
2011年10月25日 下午5:45:052011/10/25
收件者: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

未讀,
2011年10月25日 下午6:20:012011/10/25
收件者: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

未讀,
2011年10月26日 凌晨3:32:152011/10/26
收件者: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

未讀,
2011年10月26日 清晨6:53:452011/10/26
收件者: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

未讀,
2011年10月26日 下午1:03:352011/10/26
收件者: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

未讀,
2011年10月26日 下午1:06:012011/10/26
收件者: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

未讀,
2011年10月26日 下午1:16:282011/10/26
收件者:clj-...@googlegroups.com

Thanks for this !


alessio

未讀,
2011年10月26日 下午4:31:512011/10/26
收件者: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!
回覆所有人
回覆作者
轉寄
0 則新訊息