ClojureScript With React Native - File Download





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to download a file into the phone storage from a mobile application with ClojureScript and react-native. Files are already uploaded to an Amazon S3 bucket. The file URLs would be something like https://s3.amazonaws.com/client-texastech/uploads/art_pictures-images/comma-2005-06/96acad35-7f3f-4b10-a104-5bf59b92b463/medium/image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIQETNIOQ45P7UIKQ%2F20181113%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20181113T023654Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host&X-Amz-Signature=576787721283e1c2b0d8fd2349cd304b5f7f717491edb32d8d7ba256ef52f6a8



I found react-native-fetch-blob for doing it.



I tried to use it:



    (def RNFetchBlob (js/require "react-native-fetch-blob"))
(def fetch-blob (oget RNFetchBlob "default"))
(def fs (oget RNFetchBlob "fs"))

(defn download-url [app-db url]
(p/promise (fn [resolve reject]
(let [fs (oget fetch-blob "fs")
dir-path (str (oget (oget fs "dirs") "DocumentDir") "/image.png")
fetch-config (ocall fetch-blob "config" {
:addAndroidDownloads {:useDownloadManager true :path dir-path :notification true}
:Cache-Control "no-store"})
fetch-file (ocall fetch-config "fetch" "GET" url (util/get-token app-db))
base64 (oget fetch-blob "base64")]
(ocall fetch-file "then" (fn [res]
(.log js/console "Download file completed." res)
(resolve true)))
(ocall fetch-file "catch" (fn [res]
(.log js/console "Download file failed" res)
(resolve false)))
#js{:timeout 600000
:distanceFilter 1
:maximumAge 1000
:enableHighAccuracy false}))))


The download completed. But, I can't find the file in any of the folders. Is there anything wrong? If so, can anyone share the code snippet?










share|improve this question































    0















    I want to download a file into the phone storage from a mobile application with ClojureScript and react-native. Files are already uploaded to an Amazon S3 bucket. The file URLs would be something like https://s3.amazonaws.com/client-texastech/uploads/art_pictures-images/comma-2005-06/96acad35-7f3f-4b10-a104-5bf59b92b463/medium/image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIQETNIOQ45P7UIKQ%2F20181113%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20181113T023654Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host&X-Amz-Signature=576787721283e1c2b0d8fd2349cd304b5f7f717491edb32d8d7ba256ef52f6a8



    I found react-native-fetch-blob for doing it.



    I tried to use it:



        (def RNFetchBlob (js/require "react-native-fetch-blob"))
    (def fetch-blob (oget RNFetchBlob "default"))
    (def fs (oget RNFetchBlob "fs"))

    (defn download-url [app-db url]
    (p/promise (fn [resolve reject]
    (let [fs (oget fetch-blob "fs")
    dir-path (str (oget (oget fs "dirs") "DocumentDir") "/image.png")
    fetch-config (ocall fetch-blob "config" {
    :addAndroidDownloads {:useDownloadManager true :path dir-path :notification true}
    :Cache-Control "no-store"})
    fetch-file (ocall fetch-config "fetch" "GET" url (util/get-token app-db))
    base64 (oget fetch-blob "base64")]
    (ocall fetch-file "then" (fn [res]
    (.log js/console "Download file completed." res)
    (resolve true)))
    (ocall fetch-file "catch" (fn [res]
    (.log js/console "Download file failed" res)
    (resolve false)))
    #js{:timeout 600000
    :distanceFilter 1
    :maximumAge 1000
    :enableHighAccuracy false}))))


    The download completed. But, I can't find the file in any of the folders. Is there anything wrong? If so, can anyone share the code snippet?










    share|improve this question



























      0












      0








      0


      1






      I want to download a file into the phone storage from a mobile application with ClojureScript and react-native. Files are already uploaded to an Amazon S3 bucket. The file URLs would be something like https://s3.amazonaws.com/client-texastech/uploads/art_pictures-images/comma-2005-06/96acad35-7f3f-4b10-a104-5bf59b92b463/medium/image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIQETNIOQ45P7UIKQ%2F20181113%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20181113T023654Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host&X-Amz-Signature=576787721283e1c2b0d8fd2349cd304b5f7f717491edb32d8d7ba256ef52f6a8



      I found react-native-fetch-blob for doing it.



      I tried to use it:



          (def RNFetchBlob (js/require "react-native-fetch-blob"))
      (def fetch-blob (oget RNFetchBlob "default"))
      (def fs (oget RNFetchBlob "fs"))

      (defn download-url [app-db url]
      (p/promise (fn [resolve reject]
      (let [fs (oget fetch-blob "fs")
      dir-path (str (oget (oget fs "dirs") "DocumentDir") "/image.png")
      fetch-config (ocall fetch-blob "config" {
      :addAndroidDownloads {:useDownloadManager true :path dir-path :notification true}
      :Cache-Control "no-store"})
      fetch-file (ocall fetch-config "fetch" "GET" url (util/get-token app-db))
      base64 (oget fetch-blob "base64")]
      (ocall fetch-file "then" (fn [res]
      (.log js/console "Download file completed." res)
      (resolve true)))
      (ocall fetch-file "catch" (fn [res]
      (.log js/console "Download file failed" res)
      (resolve false)))
      #js{:timeout 600000
      :distanceFilter 1
      :maximumAge 1000
      :enableHighAccuracy false}))))


      The download completed. But, I can't find the file in any of the folders. Is there anything wrong? If so, can anyone share the code snippet?










      share|improve this question
















      I want to download a file into the phone storage from a mobile application with ClojureScript and react-native. Files are already uploaded to an Amazon S3 bucket. The file URLs would be something like https://s3.amazonaws.com/client-texastech/uploads/art_pictures-images/comma-2005-06/96acad35-7f3f-4b10-a104-5bf59b92b463/medium/image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIQETNIOQ45P7UIKQ%2F20181113%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20181113T023654Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host&X-Amz-Signature=576787721283e1c2b0d8fd2349cd304b5f7f717491edb32d8d7ba256ef52f6a8



      I found react-native-fetch-blob for doing it.



      I tried to use it:



          (def RNFetchBlob (js/require "react-native-fetch-blob"))
      (def fetch-blob (oget RNFetchBlob "default"))
      (def fs (oget RNFetchBlob "fs"))

      (defn download-url [app-db url]
      (p/promise (fn [resolve reject]
      (let [fs (oget fetch-blob "fs")
      dir-path (str (oget (oget fs "dirs") "DocumentDir") "/image.png")
      fetch-config (ocall fetch-blob "config" {
      :addAndroidDownloads {:useDownloadManager true :path dir-path :notification true}
      :Cache-Control "no-store"})
      fetch-file (ocall fetch-config "fetch" "GET" url (util/get-token app-db))
      base64 (oget fetch-blob "base64")]
      (ocall fetch-file "then" (fn [res]
      (.log js/console "Download file completed." res)
      (resolve true)))
      (ocall fetch-file "catch" (fn [res]
      (.log js/console "Download file failed" res)
      (resolve false)))
      #js{:timeout 600000
      :distanceFilter 1
      :maximumAge 1000
      :enableHighAccuracy false}))))


      The download completed. But, I can't find the file in any of the folders. Is there anything wrong? If so, can anyone share the code snippet?







      react-native deep-linking clojurescript react-native-fetch-blob cljsrn






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 5:35









      vikeri

      465215




      465215










      asked Oct 30 '18 at 6:01









      Tessy ThomasTessy Thomas

      1,132716




      1,132716
























          2 Answers
          2






          active

          oldest

          votes


















          0














          If you want to download and show any images form S3 you can use RN Image with source property



           [image {:source "https://your.s3.bucket/path.jpg"}]


          If it's just a file then use rn-fetch-blob



          (defn fetch-file! [url]
          (let [result (promise-chan)]
          (-> RNFetchBlob
          (.config #js{})
          (.fetch "GET" url)
          (.then #(.text %))
          (.then #(put! result [true %]))
          (.catch #(put! result [false %])))
          result))


          Make sure you have access for your S3 file






          share|improve this answer


























          • The above code is also doing the same. The control reached the then block. But, the image is not actually downloaded to any folder on my device.

            – Tessy Thomas
            Oct 30 '18 at 14:15





















          0














          Finally, I was able to download the file. The issue is where I pass parameters to the config call. We should convert Clojure to JS by using clj->js. The updated method will be as follows:



          (defn download-image-android [app-db url image-id]
          (p/promise (fn [resolve reject]
          (let [fs (oget fetch-blob "fs")
          android-dir-path (str (oget (oget fs "dirs") "DownloadDir") "/" image-id ".jpg")
          params (parse-params url)
          fetch-config (ocall fetch-blob "config" (clj->js {
          :fileCache true
          :addAndroidDownloads {:useDownloadManager true :path android-dir-path :notification true}}))
          fetch-file (ocall fetch-config "fetch" "GET" url (clj->js {:Signature (:X-Amz-Signature params)
          :Cache-Control "no-store"}))]
          (ocall fetch-file "then" (fn [res]
          (show-alert "Success" "Image has been saved to the phone successfully!")
          (resolve true)))
          (ocall fetch-file "catch" (fn [res]
          (show-alert "Error" "Image cannot be downloaded. Please try again!")
          (.log js/console "Download file failed" res)
          (resolve false)))
          #js{:timeout 30000
          :distanceFilter 1
          :maximumAge 1000
          :enableHighAccuracy false}))))





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53058250%2fclojurescript-with-react-native-file-download%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            If you want to download and show any images form S3 you can use RN Image with source property



             [image {:source "https://your.s3.bucket/path.jpg"}]


            If it's just a file then use rn-fetch-blob



            (defn fetch-file! [url]
            (let [result (promise-chan)]
            (-> RNFetchBlob
            (.config #js{})
            (.fetch "GET" url)
            (.then #(.text %))
            (.then #(put! result [true %]))
            (.catch #(put! result [false %])))
            result))


            Make sure you have access for your S3 file






            share|improve this answer


























            • The above code is also doing the same. The control reached the then block. But, the image is not actually downloaded to any folder on my device.

              – Tessy Thomas
              Oct 30 '18 at 14:15


















            0














            If you want to download and show any images form S3 you can use RN Image with source property



             [image {:source "https://your.s3.bucket/path.jpg"}]


            If it's just a file then use rn-fetch-blob



            (defn fetch-file! [url]
            (let [result (promise-chan)]
            (-> RNFetchBlob
            (.config #js{})
            (.fetch "GET" url)
            (.then #(.text %))
            (.then #(put! result [true %]))
            (.catch #(put! result [false %])))
            result))


            Make sure you have access for your S3 file






            share|improve this answer


























            • The above code is also doing the same. The control reached the then block. But, the image is not actually downloaded to any folder on my device.

              – Tessy Thomas
              Oct 30 '18 at 14:15
















            0












            0








            0







            If you want to download and show any images form S3 you can use RN Image with source property



             [image {:source "https://your.s3.bucket/path.jpg"}]


            If it's just a file then use rn-fetch-blob



            (defn fetch-file! [url]
            (let [result (promise-chan)]
            (-> RNFetchBlob
            (.config #js{})
            (.fetch "GET" url)
            (.then #(.text %))
            (.then #(put! result [true %]))
            (.catch #(put! result [false %])))
            result))


            Make sure you have access for your S3 file






            share|improve this answer















            If you want to download and show any images form S3 you can use RN Image with source property



             [image {:source "https://your.s3.bucket/path.jpg"}]


            If it's just a file then use rn-fetch-blob



            (defn fetch-file! [url]
            (let [result (promise-chan)]
            (-> RNFetchBlob
            (.config #js{})
            (.fetch "GET" url)
            (.then #(.text %))
            (.then #(put! result [true %]))
            (.catch #(put! result [false %])))
            result))


            Make sure you have access for your S3 file







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 30 '18 at 12:50

























            answered Oct 30 '18 at 12:17









            Chris CallwaitChris Callwait

            6914




            6914













            • The above code is also doing the same. The control reached the then block. But, the image is not actually downloaded to any folder on my device.

              – Tessy Thomas
              Oct 30 '18 at 14:15





















            • The above code is also doing the same. The control reached the then block. But, the image is not actually downloaded to any folder on my device.

              – Tessy Thomas
              Oct 30 '18 at 14:15



















            The above code is also doing the same. The control reached the then block. But, the image is not actually downloaded to any folder on my device.

            – Tessy Thomas
            Oct 30 '18 at 14:15







            The above code is also doing the same. The control reached the then block. But, the image is not actually downloaded to any folder on my device.

            – Tessy Thomas
            Oct 30 '18 at 14:15















            0














            Finally, I was able to download the file. The issue is where I pass parameters to the config call. We should convert Clojure to JS by using clj->js. The updated method will be as follows:



            (defn download-image-android [app-db url image-id]
            (p/promise (fn [resolve reject]
            (let [fs (oget fetch-blob "fs")
            android-dir-path (str (oget (oget fs "dirs") "DownloadDir") "/" image-id ".jpg")
            params (parse-params url)
            fetch-config (ocall fetch-blob "config" (clj->js {
            :fileCache true
            :addAndroidDownloads {:useDownloadManager true :path android-dir-path :notification true}}))
            fetch-file (ocall fetch-config "fetch" "GET" url (clj->js {:Signature (:X-Amz-Signature params)
            :Cache-Control "no-store"}))]
            (ocall fetch-file "then" (fn [res]
            (show-alert "Success" "Image has been saved to the phone successfully!")
            (resolve true)))
            (ocall fetch-file "catch" (fn [res]
            (show-alert "Error" "Image cannot be downloaded. Please try again!")
            (.log js/console "Download file failed" res)
            (resolve false)))
            #js{:timeout 30000
            :distanceFilter 1
            :maximumAge 1000
            :enableHighAccuracy false}))))





            share|improve this answer




























              0














              Finally, I was able to download the file. The issue is where I pass parameters to the config call. We should convert Clojure to JS by using clj->js. The updated method will be as follows:



              (defn download-image-android [app-db url image-id]
              (p/promise (fn [resolve reject]
              (let [fs (oget fetch-blob "fs")
              android-dir-path (str (oget (oget fs "dirs") "DownloadDir") "/" image-id ".jpg")
              params (parse-params url)
              fetch-config (ocall fetch-blob "config" (clj->js {
              :fileCache true
              :addAndroidDownloads {:useDownloadManager true :path android-dir-path :notification true}}))
              fetch-file (ocall fetch-config "fetch" "GET" url (clj->js {:Signature (:X-Amz-Signature params)
              :Cache-Control "no-store"}))]
              (ocall fetch-file "then" (fn [res]
              (show-alert "Success" "Image has been saved to the phone successfully!")
              (resolve true)))
              (ocall fetch-file "catch" (fn [res]
              (show-alert "Error" "Image cannot be downloaded. Please try again!")
              (.log js/console "Download file failed" res)
              (resolve false)))
              #js{:timeout 30000
              :distanceFilter 1
              :maximumAge 1000
              :enableHighAccuracy false}))))





              share|improve this answer


























                0












                0








                0







                Finally, I was able to download the file. The issue is where I pass parameters to the config call. We should convert Clojure to JS by using clj->js. The updated method will be as follows:



                (defn download-image-android [app-db url image-id]
                (p/promise (fn [resolve reject]
                (let [fs (oget fetch-blob "fs")
                android-dir-path (str (oget (oget fs "dirs") "DownloadDir") "/" image-id ".jpg")
                params (parse-params url)
                fetch-config (ocall fetch-blob "config" (clj->js {
                :fileCache true
                :addAndroidDownloads {:useDownloadManager true :path android-dir-path :notification true}}))
                fetch-file (ocall fetch-config "fetch" "GET" url (clj->js {:Signature (:X-Amz-Signature params)
                :Cache-Control "no-store"}))]
                (ocall fetch-file "then" (fn [res]
                (show-alert "Success" "Image has been saved to the phone successfully!")
                (resolve true)))
                (ocall fetch-file "catch" (fn [res]
                (show-alert "Error" "Image cannot be downloaded. Please try again!")
                (.log js/console "Download file failed" res)
                (resolve false)))
                #js{:timeout 30000
                :distanceFilter 1
                :maximumAge 1000
                :enableHighAccuracy false}))))





                share|improve this answer













                Finally, I was able to download the file. The issue is where I pass parameters to the config call. We should convert Clojure to JS by using clj->js. The updated method will be as follows:



                (defn download-image-android [app-db url image-id]
                (p/promise (fn [resolve reject]
                (let [fs (oget fetch-blob "fs")
                android-dir-path (str (oget (oget fs "dirs") "DownloadDir") "/" image-id ".jpg")
                params (parse-params url)
                fetch-config (ocall fetch-blob "config" (clj->js {
                :fileCache true
                :addAndroidDownloads {:useDownloadManager true :path android-dir-path :notification true}}))
                fetch-file (ocall fetch-config "fetch" "GET" url (clj->js {:Signature (:X-Amz-Signature params)
                :Cache-Control "no-store"}))]
                (ocall fetch-file "then" (fn [res]
                (show-alert "Success" "Image has been saved to the phone successfully!")
                (resolve true)))
                (ocall fetch-file "catch" (fn [res]
                (show-alert "Error" "Image cannot be downloaded. Please try again!")
                (.log js/console "Download file failed" res)
                (resolve false)))
                #js{:timeout 30000
                :distanceFilter 1
                :maximumAge 1000
                :enableHighAccuracy false}))))






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 12 at 8:43









                Tessy ThomasTessy Thomas

                1,132716




                1,132716






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53058250%2fclojurescript-with-react-native-file-download%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)