将文件从 URL 下载到用户可访问的位置

RSM*_*RSM 5 http typescript nativescript angular2-nativescript angular

我正在使用Nativescript/Angular 2构建应用程序

我希望能够从 URL 下载文件并将其保存到设备中普通用户可以轻松找到它的位置。我相信,下载将是这两个最好的地方iOSAndroid。如果我错了,请纠正我。

该文件可以是任何文件类型,而不仅仅是图像。所以主要是spreadsheetword documentpdfpngjpg,等。

我在网上和文档中搜索过。该文档描述了一种名为getFile的方法,该方法获取文件并将其保存到您的设备。

我在我的代码中实现了这一点,如下所示:

download (id) {
  console.log('Download Started');
  getFile("https://raw.githubusercontent.com/NativeScript/NativeScript/master/apps/tests/logo.png").then(function (r) {
    console.log(r.path);
  }, function (e) {
    //// Argument (e) is Error!
  });
}
Run Code Online (Sandbox Code Playgroud)

这样做的问题是它将它保存到非用户可访问的位置,例如:

/data/user/0/com.myapp.example/files/logo.png
Run Code Online (Sandbox Code Playgroud)

更新:

我也试过直接指定路径:

fs.knownFolders.documents();
Run Code Online (Sandbox Code Playgroud)

但是,此方法获取用户或外部应用程序无法访问的当前应用程序的文档文件夹

And*_*fin 12

经过一些不成功的尝试,我终于找到了如何将文件保存到用户“下载”文件夹(类似于 sdcard/下载)。您可以使用 android.os.Environment 方法来获取此文件夹。

在你的组件中添加这个:

import { getFile } from 'tns-core-modules/http';
import * as fileSystem from "tns-core-modules/file-system";
import { isAndroid } from "tns-core-modules/platform";
import { alert } from "tns-core-modules/ui/dialogs";

declare var android;
Run Code Online (Sandbox Code Playgroud)

<...>

public download (url, fileName) {
    if (isAndroid) {
        const permissions = require("nativescript-permissions");
        permissions.requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, "I need these permissions because I'm cool")
            .then(() => {
                let downloadedFilePath = fileSystem.path.join(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), fileName);
                getFile(url, downloadedFilePath).then(resultFile => {
                    alert({
                        title: 'Saved!',
                        okButtonText: 'OK',
                        message: `File saved here:\n${resultFile.path}`
                    });
                }, error => {
                    alert({
                        title: 'Error',
                        okButtonText: 'OK',
                        message: `${error}`
                    });
                });
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

你还应该知道的:

1) 没有任何下载指示器,也没有出现标准系统下载栏,我不知道如何解决这个问题。

2)对于iOS,您可以尝试使用

const filePath = fileSystem.path.join(fileSystem.knownFolders.ios.downloads().path, fileName);
getFile(url, filePath).then((resultFile) => {}, (error) => {});
Run Code Online (Sandbox Code Playgroud)

我认为,遗憾的是 NS 文档不直言不讳,您无法仅使用 NS 功能将文件保存在用户可访问的位置。我只有在阅读文件 /node_modules/tns-core-modules/file-system/file-system.d.ts 中的评论时才明白

希望这对你有帮助。


ale*_*nco 1

同一文档说您可以像这样指定文件位置:

download (id) {
  console.log('Download Started');
  var folder = fs.knownFolders.documents();
  var file = fs.path.join(folder.path, "logo.png");
  var url = "https://raw.githubusercontent.com/NativeScript/NativeScript/master/apps/tests/logo.png"
  getFile(url, file).then(function (r) {
    console.log(r.path);
  }, function (e) {
    //// Argument (e) is Error!
  });
}
Run Code Online (Sandbox Code Playgroud)

免责声明:我自己从未尝试过,只是阅读文档......