uni.chooseFile()在鸿蒙Next系统上返回的路径在uni-app uts插件中用fs.accessSync()查询返回为false

uni.chooseFile()在鸿蒙Next系统上返回的路径在uni-app uts插件中用fs.accessSync()查询返回为false

示例代码:

uni.chooseFile({
count: 1, //默认100
type: "all",
success: function (res) {
let sourceFilePath = res.tempFiles[0].path;
//  例如:"file://docs/storage/Users/currentUser/Documents/%E6%AC%A2%E8%BF%8E%E4%BD%BF%E7%94%A8%E5%8D%8E%E4%B8%BA%E6%96%87%E4%BB%B6%E7%AE%A1%E7%90%86.pdf"
// res.tempFiles
}
})
//uts 插件
import { fileIo as fs } from '@kit.CoreFileKit';
fs.accessSync(sourceFilePath);

操作步骤:

通过uni.chooseFile()调用文件选择器,但是在uts插件中,查询该文件路径时,返回没有这个文件

预期结果:

可以返回正确的路径,可以通过fs.accessSync() 正确的查询到该文件地址

实际结果:

并不能查询到该文件

bug描述:

uni.chooseFile()在鸿蒙Next系统上,返回的路径,在uts插件中,用fs.accessSync() 查询,返回为false


更多关于uni.chooseFile()在鸿蒙Next系统上返回的路径在uni-app uts插件中用fs.accessSync()查询返回为false的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni.chooseFile()在鸿蒙Next系统上返回的路径在uni-app uts插件中用fs.accessSync()查询返回为false的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


这个问题的原因是鸿蒙Next系统下uni.chooseFile()返回的路径格式与UTS插件期望的格式不一致。在鸿蒙系统上返回的是带"file://"前缀的URI格式路径,而UTS的CoreFileKit模块需要的是实际的文件系统路径。

解决方案是去掉路径中的"file://"前缀,可以使用以下方法处理:

let sourceFilePath = res.tempFiles[0].path.replace('file://', '');

或者在UTS插件中处理路径:

import { fileIo as fs } from '[@kit](/user/kit).CoreFileKit';
let realPath = sourceFilePath.replace('file://', '');
fs.accessSync(realPath);
回到顶部