Home > Web Front-end > uni-app > How does uniapp determine whether a local file exists?

How does uniapp determine whether a local file exists?

PHPz
Release: 2023-04-17 11:19:53
Original
2626 people have browsed it

With the development of mobile Internet technology, more and more applications need to read and write local files, which requires us to perform file operations during the development process. As a cross-platform development framework, uniapp also provides a rich API to operate local files. When operating local files, we often need to determine whether the file exists. So in uniapp, how do we determine whether the local file exists?

In uniapp, we can use the uni.getFileInfo() method to obtain file information to determine whether the file exists. Its definition is as follows:

uni.getFileInfo({
    filePath: '', // 必填,临时文件路径,不支持 res: 开头的文件路径。
    success: res => {}, // 必填,获取成功的回调函数
    fail: () => {}, // 必填,获取失败的回调函数
    complete: () => {} // 非必填,API 调用结束的回调函数(调用成功、失败都会执行)
})
Copy after login

By calling this method, we can obtain the size, creation time, modification time and other information of the file. If the file does not exist, the fail callback function is triggered. We can perform file operations in the success callback function, such as file upload, download, etc.

Then we can determine whether the local file exists through the following method:

uni.getFileInfo({
    filePath: '/storage/emulated/0/test.txt',
    success: res => {
        console.log('file exist', res.size); // 输出文件大小
    },
    fail: err => {
        console.log('file not exist', err); // 输出错误信息
    }
});
Copy after login

In the above code, we determine the locally stored /storage/emulated/0/test.txt Whether the file exists. If the file exists, the file size is output; if it does not exist, an error message is output.

In addition, we can also use the uni.getSavedFileList() method to get a list of all saved files to determine whether the file exists. It is defined as follows:

uni.getSavedFileList({
    success: res => {}, // 必填,获取成功的回调函数
    fail: () => {}, // 必填,获取失败的回调函数
    complete: () => {} // 非必填,API 调用结束的回调函数
})
Copy after login

This method will return a list of all saved files, including file path, file size and other information. We just need to traverse this list to find whether the specified file exists.

To sum up, we can use the uni.getFileInfo() or uni.getSavedFileList() method to determine whether the local file exists. In actual development, we can choose the appropriate method to perform file operations as needed.

The above is the detailed content of How does uniapp determine whether a local file exists?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template