1
|
cordova plugin add cordova-plugin-file
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta http-equiv=
"Content-type"
content=
"text/html; charset=utf-8"
>
<script type=
"text/javascript"
charset=
"utf-8"
src=
"cordova.js"
></script>
<script type=
"text/javascript"
charset=
"utf-8"
>
//创建并写入文件
function
createAndWriteFile(){
//持久化数据保存
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function
(fs) {
console.log(
'打开的文件系统: '
+ fs.name);
fs.root.getFile(
"hangge.txt"
, { create:
true
, exclusive:
false
},
function
(fileEntry) {
console.log(
"是否是个文件?"
+ fileEntry.isFile.toString());
// fileEntry.name == 'hangge.txt'
// fileEntry.fullPath == '/hangge.txt'
//文件内容
var
dataObj =
new
Blob([
'欢迎访问hangge.com'
], { type:
'text/plain'
});
//写入文件
writeFile(fileEntry,
null
);
}, onErrorCreateFile);
}, onErrorLoadFs);
}
//将内容数据写入到文件中
function
writeFile(fileEntry, dataObj) {
//创建一个写入对象
fileEntry.createWriter(
function
(fileWriter) {
//文件写入成功
fileWriter.onwriteend =
function
() {
console.log(
"Successful file read..."
);
};
//文件写入失败
fileWriter.onerror =
function
(e) {
console.log(
"Failed file read: "
+ e.toString());
};
//写入文件
fileWriter.write(dataObj);
});
}
//文件创建失败回调
function
onErrorCreateFile(error){
console.log(
"文件创建失败!"
)
}
//FileSystem加载失败回调
function
onErrorLoadFs(error){
console.log(
"文件系统加载失败!"
)
}
</script>
</head>
<body style=
"padding-top:50px"
>
<button style=
"font-size:23px;"
onclick=
"createAndWriteFile();"
>创建并写入文件</button>
</body>
</html>
|
1
|
<
preference
name
=
"iosPersistentFileLocation"
value
=
"Compatibility"
/>
|
1
|
<
preference
name
=
"iosPersistentFileLocation"
value
=
"Library"
/>
|
1
|
<
preference
name
=
"AndroidPersistentFileLocation"
value
=
"Internal"
/>
|
1
|
<
preference
name
=
"AndroidPersistentFileLocation"
value
=
"Compatibility"
/>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//创建并写入文件
function
createAndWriteFile(){
//临时数据保存
window.requestFileSystem(LocalFileSystem.TEMPORARY, 5 * 1024 * 1024,
function
(fs) {
console.log(
'打开的文件系统: '
+ fs.name);
fs.root.getFile(
"hangge.txt"
, { create:
true
, exclusive:
false
},
function
(fileEntry) {
console.log(
"是否是个文件?"
+ fileEntry.isFile.toString());
// fileEntry.name == 'hangge.txt'
// fileEntry.fullPath == '/hangge.txt'
//文件内容
var
dataObj =
new
Blob([
'欢迎访问hangge.com'
], { type:
'text/plain'
});
//写入文件
writeFile(fileEntry, dataObj);
}, onErrorCreateFile);
}, onErrorLoadFs);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//将内容数据写入到文件中(支持追加内容)
function
writeFile(fileEntry, dataObj, isAppend) {
//创建一个写入对象
fileEntry.createWriter(
function
(fileWriter) {
//文件写入成功
fileWriter.onwriteend =
function
() {
console.log(
"Successful file read..."
);
};
//文件写入失败
fileWriter.onerror =
function
(e) {
console.log(
"Failed file read: "
+ e.toString());
};
//如果是最加内容,则定位到文件尾部
if
(isAppend) {
try
{
fileWriter.seek(fileWriter.length);
}
catch
(e) {
console.log(
"file doesn't exist!"
);
}
}
fileWriter.write(dataObj);
});
}
|
1
2
|
var
dataObj =
new
Blob([
'\n值得您每天来看看!'
], { type:
'text/plain'
});
writeFile(fileEntry, dataObj,
true
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//读取文件
function
readFile(fileEntry) {
fileEntry.file(
function
(file) {
var
reader =
new
FileReader();
reader.onloadend =
function
() {
alert(
this
.result);
};
reader.readAsText(file);
}, onErrorReadFile);
}
//读取文件失败响应
function
onErrorReadFile(){
console.log(
"文件读取失败!"
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//创建并写入文件
function createAndWriteFile(){
//临时数据保存
window.requestFileSystem(
LocalFileSystem
.
PERSISTENT
, 0, function (fs) {
console.log(
'打开的文件系统: '
+ fs.name);
fs.root.getDirectory(
'assets'
, { create:
true
}, function (dirEntry) {
dirEntry.getDirectory(
'images'
, { create:
true
}, function (subDirEntry) {
//createFile(subDirEntry, "hangge.txt");
}, onErrorGetDir);
}, onErrorGetDir);
}, onErrorLoadFs);
}
//文件夹创建失败回调
function onErrorGetDir(error){
console.log(
"文件夹创建失败!"
)
}
//FileSystem加载失败回调
function onErrorLoadFs(error){
console.log(
"文件系统加载失败!"
)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta http-equiv=
"Content-type"
content=
"text/html; charset=utf-8"
>
<script type=
"text/javascript"
charset=
"utf-8"
src=
"cordova.js"
></script>
<script type=
"text/javascript"
charset=
"utf-8"
>
//下载图片
function
downloadImage(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function
(fs) {
console.log(
'打开的文件系统: '
+ fs.name);
getSampleFile(fs.root);
}, onErrorLoadFs);
}
//获取图片
function
getSampleFile(dirEntry) {
var
xhr =
new
XMLHttpRequest();
xhr.responseType =
'blob'
;
xhr.onload =
function
() {
if
(
this
.status == 200) {
var
blob =
new
Blob([
this
.response], { type:
'image/png'
});
saveFile(dirEntry, blob,
"hangge.png"
);
}
};
xhr.send();
}
//保存图片文件
function
saveFile(dirEntry, fileData, fileName) {
dirEntry.getFile(fileName, { create:
true
, exclusive:
false
},
function
(fileEntry) {
writeFile(fileEntry, fileData);
}, onErrorCreateFile);
}
//将图片数据写入到文件中
function
writeFile(fileEntry, dataObj, isAppend) {
//创建一个写入对象
fileEntry.createWriter(
function
(fileWriter) {
//文件写入成功
fileWriter.onwriteend =
function
() {
console.log(
"Successful file write..."
);
if
(dataObj.type ==
"image/png"
) {
readBinaryFile(fileEntry);
}
else
{
readFile(fileEntry);
}
};
//文件写入失败
fileWriter.onerror =
function
(e) {
console.log(
"Failed file write: "
+ e.toString());
};
//写入文件
fileWriter.write(dataObj);
});
}
//文件创建失败回调
function
onErrorCreateFile(error){
console.log(
"文件创建失败!"
)
}
//FileSystem加载失败回调
function
onErrorLoadFs(error){
console.log(
"文件系统加载失败!"
)
}
</script>
</head>
<body style=
"padding-top:50px"
>
<button style=
"font-size:23px;"
onclick=
"downloadImage();"
>下载图片</button>
</body>
</html>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta http-equiv=
"Content-type"
content=
"text/html; charset=utf-8"
>
<script type=
"text/javascript"
charset=
"utf-8"
src=
"cordova.js"
></script>
<script type=
"text/javascript"
charset=
"utf-8"
>
//加载图片
function
loadImage(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function
(fs) {
console.log(
'打开的文件系统: '
+ fs.name);
fs.root.getFile(
"hangge.png"
, { create:
true
, exclusive:
false
},
function
(fileEntry) {
readBinaryFile(fileEntry);
}, onErrorCreateFile);
}, onErrorLoadFs);
}
//读取图片文件
function
readBinaryFile(fileEntry) {
fileEntry.file(
function
(file) {
var
reader =
new
FileReader();
reader.onloadend =
function
() {
//加载成功显示图片
var
blob =
new
Blob([
new
Uint8Array(
this
.result)], { type:
"image/png"
});
displayImage(blob);
};
reader.readAsArrayBuffer(file);
}, onErrorReadFile);
}
//显示图片
function
displayImage(blob) {
var
elem = document.getElementById(
'imageFile'
);
elem.src = window.URL.createObjectURL(blob);
}
//文件创建失败回调
function
onErrorCreateFile(error){
console.log(
"文件创建失败!"
)
}
//读取文件失败响应
function
onErrorReadFile(){
console.log(
"文件读取失败!"
);
}
//FileSystem加载失败回调
function
onErrorLoadFs(error){
console.log(
"文件系统加载失败!"
)
}
</script>
</head>
<body style=
"padding-top:50px"
>
<button style=
"font-size:23px;"
onclick=
"loadImage();"
>加载图片</button>
<image id=
"imageFile"
/>
</body>
</html>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta http-equiv=
"Content-type"
content=
"text/html; charset=utf-8"
>
<script type=
"text/javascript"
charset=
"utf-8"
src=
"cordova.js"
></script>
<script type=
"text/javascript"
charset=
"utf-8"
>
//加载图片
function
loadImage(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function
(fs) {
console.log(
'打开的文件系统: '
+ fs.name);
fs.root.getFile(
"hangge.png"
, { create:
true
, exclusive:
false
},
function
(fileEntry) {
displayImageByFileURL(fileEntry);
}, onErrorCreateFile);
}, onErrorLoadFs);
}
//显示图片
function
displayImageByFileURL(fileEntry) {
var
elem = document.getElementById(
'imageFile'
);
elem.src = fileEntry.toURL();
}
//文件创建失败回调
function
onErrorCreateFile(error){
|