【问题标题】:Ionic File Chooser URL issue离子文件选择器 URL 问题
【发布时间】:2019-03-01 21:50:10
【问题描述】:

我正在使用这个插件来获取文件
https://ionicframework.com/docs/native/file-chooser/

当我从 Downloads 文件夹中选择一个 PDF 文件时,它会给我一个 URI,例如 content://com.android.providers.downloads.documents/document/1015

但如果我选择另一个文件,如 ZIPPNG,它会解析为合适的路径

content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fwws.zip

content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fopacity.png

如何从file://…形式的文件中获取路径。

我尝试了以下方法,但没有成功:

FilePath.resolveNativePath

normalizeURL

更新:

进口

import { File } from '@ionic-native/file';
import { FileChooser } from '@ionic-native/file-chooser'
import { FilePath } from '@ionic-native/file-path';

文件选择器代码:

  getAndroidFile() {

    this.fileChooser.open()
      .then(uri => {
        console.log("uri", uri);
        this.file.resolveLocalFilesystemUrl(uri).then((files) => {
          console.log("files", files);
        }).catch((error) => { console.log("error", error) });
        (<any>window).FilePath.resolveNativePath(uri, (result) => {
          this.nativepath = result;
          console.log("nativepath", this.nativepath);
        }, (err) => {
          console.log("err", err);
        })
      })
  }

我按照 Aaron 的建议使用它:

this.file.resolveLocalFilesystemUrl(uri).then((files)=>{
          console.log("files",files);
        }).catch((error) => { console.log("error", error) });

它仍然返回:

filesystem: FileSystem {name: "content", root: DirectoryEntry}
fullPath: "/com.android.providers.downloads.documents/document/1015"
isDirectory: false
isFile: true
name: "1015"
nativeURL: "content://com.android.providers.downloads.documents/document/1015"

哪个是原始网址。

更新 2:

getBase64Content(nativepath) {
let path = nativepath.substring(0, nativepath.lastIndexOf('/'));
let filename = nativepath.substring(nativepath.lastIndexOf('/'), nativepath.length);
filename = filename.replace("/", "");
console.log("path", path);
console.log("filename", filename);
this.file.readAsDataURL(path, filename)
  .then(content => {
    console.log('content1', content);
    //this will be passed to web API 
    this.base64content = content;

  })
  .catch(err => {
    console.log("err", err);
  })

}

这个 base64 内容通过 api 发送到服务器,然后他们将其转换回文件。

更新 3: 抱歉回复晚了,真的忙于另一个项目。 Koken的答案:

你如何测试这个东西? 你使用什么命令来执行? 离子科尔多瓦运行android (我通过使用 chrome 进行远程调试来检查值。)

安卓模拟器?安卓设备? 安卓设备。红米note 5 pro(MIUI 10, Android 8.1)。

另外...选择文件后的下一步是什么? 此 base64 内容通过 api 发送到服务器,然后他们将其转换回文件。

希望你们能帮助我。给出代码。谢谢

【问题讨论】:

    标签: android ionic-framework ionic3 ionic-native


    【解决方案1】:

    您需要来自FileManager 插件的resolveLocalFilesystemUrl

    https://ionicframework.com/docs/native/file/#resolveLocalFilesystemUrl

    【讨论】:

    • 嗨亚伦我已经按照你说的做了,但得到的路径与响应相同。你可以在这里查看我的答案link
    猜你喜欢
    • 2015-10-22
    • 2016-09-26
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2012-08-17
    • 2021-02-09
    相关资源
    最近更新 更多