"; */ ?>

Convert HTML5 FileList to Clojure Vector

When AJAX uploading data/files via input elements with ClojureScript, HTML5 returns FileList which is not really a list nor an array, hence can not be converted to Clojure by simply calling “js->clj“, although the usage is pretty similar to an array:

// uploadData is a form element
// fileChooser is input element of type 'file'
var file = document.forms['uploadData']['fileChooser'].files[0];

But as it is always in the Clojure world, there is a solution. This one is not trivial, but after spending almost 30 minutes, it surfaced under my finger tips:

(defn toArray [js-col]
  (-> (clj->js []) 
      (.-slice)
      (.call js-col)
      (js->clj)))

Now in ClojureScript the “(toArray fileList)” will return a classic seq that can be loved and iterated.

Tags: ,