SoFunction
Updated on 2025-03-01

Uniapp storing data to local files and reading local file data code examples

Possible problems:

1. A higher version of Android system may not see the file in the file system but can be read.

2. This method may have problems in Android 10 system

1. Read local files

        // Method for reading local files Parameter 1 is the file name, and parameter 2 is the callback method used to process the obtained file data		fetchFileData(fileName,callback) {
			(
				.PUBLIC_DOWNLOADS, // Root directory in the file system				fs => {
					// Create or open a file, which is the root directory operation object, directly fs represents the current operation object					(fileName, {
						create: true // Create if the file does not exist					}, fileEntry => {
						// The path of the file in the phone						('Path of file in phone', )
						(function(file) {
							var fileReader = new ();
							("getFile:" + (file));
							(file, "utf-8")
							 = function(res) { //Callback event after successful reading of file content								//Readed file content information								('The last reading of the file is=======', )
								callback()
							}
						});
					}, e => {
						('Error 111', "getFile failed: " + );
					});
				},
				e => {
					('Error 222', );
				}
			);
		},

2. Store data to local files

// Parameter 1 is the file name Parameter 2 is the file content Parameter 3 is the callback parametersaveFile(fileName,fileInfo,callback) {
			// Request local file system object			(
				.PUBLIC_DOWNLOADS, // Root directory in the file system				fs => {
					// Create or open a file, which is the root directory operation object, directly fs represents the current operation object					(fileName, {
						create: true // Create if the file does not exist					}, fileEntry => {
						// The path of the file in the phone						('The path to the last file in the phone========', )
						(writer => {
							// Callback function that successfully completes writing to the file							 = e => {
								("Writing data successfully");
								callback(true)
							};
							// Write data							((fileInfo));
						})
					}, e => {
						callback(false)
						('Error 111', "getFile failed: " + );
					});
				},
				e => {
					('Error 222', );
					callback(false)
				}
			);
		},

Summarize

This is the article about uniapp storing data to local files and reading local file data. For more relevant uniapp storing data to local files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!