I would like to open an excel file from multipart data without saving
it (forbidden by Google Appengine). I have adapted the incanter's
read-xls which reads from actual files to read-xls-stream which takes
an object of the same kind that with-open provides.
I wanted to write something like the following but I get the error
message: "clojure.lang.PersistentVector cannot be cast to
java.lang.String". I don't really understand the mismatch and how to
convert one type to the other...
(use [appengine-magic.multipart-params :only (wrap-multipart-params)])
(defn retailer-stock-update []
(wrap-multipart-params
(fn [req]
(let [xls (get (:params req) "stock-xls")]
(with-in-str [str (:bytes xls)]
(read-xls-stream str))))))
As far as I have understood, (:bytes xls) is seen as a java byte
array, since (class (:bytes xls)) displays "class [B", but I don't get
how it is related to the previous error message. I guess I sould
manually convert (:bytes xls) to the type with-in-str wants but I
don't know how.
I have given a look to
http://code.google.com/appengine/kb/java.html#fileforms but I guess
clojure can do without explicit call to FileItemStream and
FileItemIterator. Anyway, I didn't manage to deal with it either.
Could you give me a clue?
Thanks in advance,
Vincent
(defn retailer-stock-update []
(wrap-multipart-params
(fn [req]
(let [xls (get (:params req) "stock-xls")]
(with-in-str [str (:bytes xls)]
(read-xls-stream str))))))
That is correct. So try replacing the last two lines with:
(with-open [str (ByteArrayInputStream. (:bytes xls))]
(read-xls-stream str))
The exception is because with-in-str is expecting a single string
argument, not a binding vector; with-in-str temporarily rebinds *in*
to read from a string rather than binding an arbitrary stream.
--
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.
You're right, thanks. I noticed this too late.
Thank you! I eventually noticed my error on the with-in-str call, but
I would never have found ByteArrayInputStream. The clojure newcomer
should be ready to learn java :) Many thanks for your help!
Vincent
You're welcome.