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){
|