Saving a file

476 views
Skip to first unread message

Jensontech

unread,
Feb 13, 2015, 2:05:37 AM2/13/15
to clojur...@googlegroups.com
I need to allow the user to save content generated by Clojurescript to their local drive.

I do it using this code -

(defn- save-file
[filename content]
(let [lnk (get-element-by-id "file-export-link")
blob (js/Blob. (js/Array. content) {:type "text/plain"})]
(set! (.-href lnk) (.createObjectURL js/URL blob))
(set! (.-download lnk) filename)
(.click lnk)))

When the function runs it does a pseudo download to the named file. This works with ":optimizations :none".

When I try the same thing with ":optimizations :advanced", instead of doing a download and prompting to open or save a file, it moves the browser to a URL like: "blob:null/d9bbcdab-7c87-....etc"

I have tried Chas Emericks's url package https://github.com/cemerick/url to create the URL but it still doesn't work.

Something is being munged by the optimization, but I am not sure what.
Does anyone have any ideas how to go about fixing this?

Mike Thompson

unread,
Feb 14, 2015, 8:35:38 PM2/14/15
to clojur...@googlegroups.com
I hesitate to offer advice here, because I'm just finding my way in these sort of matters myself but ...

1. You are using HTML5 APIs: Blob and URL.
2. Your code works with optimisation :none, but not :advanced
3. I conclude that advanced optimisation is munging the symbols.
4. Which means you need to include externs for this API for URL and Blob)

This appears to be the right set of externs:
https://code.google.com/p/closure-compiler/source/browse/externs/fileapi.js

But at that point, I run out of knowing much. Because it is part of Google Closure, you'd think there was some way of pulling in that file.

Background: http://lukevanderhart.com/2011/09/30/using-javascript-and-clojurescript.html


Alternative thought ...

Perhaps you could the `goog` wrappers for the "HTML5 API":
http://docs.closure-library.googlecode.com/git/namespace_goog_fs.html

(But I can't see how to get a Blob).

(ns my.module
(:require [goog.fs :as fs]))

;; later
(.createObjectUrl fs blob)

;; or maybe
(fs/createObjectUrl blob)

;; BUT no idea how to create a Blob in the first place.

--
Mike



--
Mike



Immo Heikkinen

unread,
Feb 16, 2015, 1:30:07 AM2/16/15
to clojur...@googlegroups.com
The problem is `download` property that gets munged in advanced compilation. 

You can use `aset` to prevent it from being munged:

    (aset lnk "download" filename)

or alternatively provide extern file to tell Closure compiler not to munge it:

function HTMLLinkElement() {}
HTMLLinkElement.proptotype.download;







--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to the Google Groups "ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Reply all
Reply to author
Forward
0 new messages