如何通过fetch/axios获取本地文件?

问题描述 投票:0回答:1

出于学习原因,我需要使用其中一些network API,例如fetchaxios,但要获取LOCAL文件,因此WITHOUT使用fs模块,或WITHOUT导入它们。

我尝试了 fetch 和 axios,但他们没有实现这个“功能”来获取本地文件,也是因为我认为它不太安全。

获取:

return new Promise((resolve, reject) => {
    fetch('file://users.js')
    .then(data => {
        console.log(data);
        resolve(data);
    })
    .catch(error => {
        console.error('Errore durante il recupero del file:', error);
        reject(error);
    });
});

我收到此错误:

原因:错误:尚未实施...尚未...

axios:

return new Promise((resolve, reject) => {
        axios.get('file://users.js')
        .then(response => {
            console.log(response.data);
            resolve(response.data);
        })
        .catch(error => {
            console.error('Errore durante il recupero del file:', error);
            reject(error);
        });
    });

我收到此错误:

生成的消息:假, 代码:'ERR_ASSERTION', 实际:'文件:', 预期:'http:', 运算符:'=='

有其他网络 API 可以让我这样做吗?

javascript node.js ajax axios fetch-api
1个回答
0
投票

创建一个浏览器扩展,在

"host_permissions"
中设置
file://*/*
,然后就可以使用绝对路径来获取
file:
协议。

© www.soinside.com 2019 - 2024. All rights reserved.