I tried sending a jpg image through a websocket using the HTML5
FileReader object (reader.readAsArrayBuffer(f)).
The first 1010 bytes transferred perfectly. 1010 is the size of the
first Java.nio.HeapByteBuffer in the
gloss.data.bytes.core.MultiBufferSequence. After that, nothing in the
hexdumps seems to match.
I'd like to simply decode the binary and write it to a file, but the
image data seem to have been corrupted after the first ByteBuffer.
So my questions are:
1. Is there an obvious and easy way to decode and write binary to a
file from one of Aleph's websocket handlers?
2. is there a non-obvious hard way to that?
Any tips or tricks would be most appreciated.
BTW, it would be nice if you could just do
(receive ch
#(
clojure.java.io/copy % (as-file destination)))
Anyway, here is my latest hackings below..... thanks so much for the
help in advance.
** The client-side Javascript code.
var handle_file_select = function(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files; // FileList object.
// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
if (!f.type.match('image.*')){ continue; }
var reader = new FileReader();
reader.onload = (function(the_file) {
return function(e){
ws['upload'].send(e.target.result);
}
})(f);
reader.readAsArrayBuffer(f);
}// for f
}// handle_file_select
; The server-side code.
(ns chat.upload
(:use lamina.core)
(:use aleph.http)
(:use
gloss.io))
(defn upload-handler [user-ch handshake]
(receive user-ch
(fn [client_id]
(let [dest (str "/var/www/static/original/" client_id "/
sample.jpg")]
(receive user-ch
(fn [msg]
(let [fout (-> dest
java.io.FileOutputStream. .getChannel)
buf (contiguous msg)
dbuf (java.nio.ByteBuffer/allocateDirect(.remaining
buf))]
(do
(-> dbuf (.put buf) .flip)
(.write fout dbuf)
(.close fout)))))))))
(def s3 (start-http-server upload-handler {:port 9013 :websocket
true}))