ClojureScript 与 React Native - 文件下载
ClojureScript With React Native - File Download
我想使用 ClojureScript 和 react-native 从移动应用程序将文件下载到 phone 存储中。文件已上传到 Amazon S3 存储桶。文件 URL 类似于 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
我找到了 react-native-fetch-blob 来做这件事。
我尝试使用它:
(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}))))
下载完成。但是,我无法在任何文件夹中找到该文件。有什么问题吗?如果是这样,任何人都可以分享代码片段吗?
如果您想下载和显示来自 S3 的任何图像,您可以使用 RN Image 和来源 属性
[image {:source "https://your.s3.bucket/path.jpg"}]
如果它只是一个文件,那么使用 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))
确保您有权访问 S3 文件
我终于可以下载文件了。问题是我将参数传递给配置调用的地方。我们应该使用 clj->js 将 Clojure 转换为 JS。更新后的方法如下:
(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}))))
我想使用 ClojureScript 和 react-native 从移动应用程序将文件下载到 phone 存储中。文件已上传到 Amazon S3 存储桶。文件 URL 类似于 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
我找到了 react-native-fetch-blob 来做这件事。
我尝试使用它:
(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}))))
下载完成。但是,我无法在任何文件夹中找到该文件。有什么问题吗?如果是这样,任何人都可以分享代码片段吗?
如果您想下载和显示来自 S3 的任何图像,您可以使用 RN Image 和来源 属性
[image {:source "https://your.s3.bucket/path.jpg"}]
如果它只是一个文件,那么使用 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))
确保您有权访问 S3 文件
我终于可以下载文件了。问题是我将参数传递给配置调用的地方。我们应该使用 clj->js 将 Clojure 转换为 JS。更新后的方法如下:
(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}))))