HTML5——FileAPI(二):FileSystem API上

Day9

今日份内容:FileSystem API的使用

当应用程序中需要使用大量的二进制数据或其他非Web程序共享数据时,本地数据库并不能满足用户的需求,可以使用FileSystem API来解决上述问题,将用户的数据临时或永久的存储到计算机中。

FileSystem API

  • 申请磁盘配额
  • 访问本地文件系统
  • 文件操作

内容

创建文件、写入文件、文件追加数据、读取文件、删除文件

代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>getFile创建文件</title>
		<style type="text/css">
			div {
    
				display: block;
			}

			#hBlock {
    
				color: bisque;
				background-color: coral;
				height: 5px;
				border: solid;

			}
		</style>
	</head>
	<body>
		<!-- 请求访问本地文件系统 -->
		<!-- <div id="">
		<input type="button" value="发出请求" οnclick="createFile()" /><hr >
		<output id="result"></output>
		<script type="text/javascript">
			window.requestFileSystem = window.requestFileSystem||window.webkitRequestFileSystem;
			navigator.webkitPersistentStorage.requestQuota(1024*1024,
				function(grantedBytes){
					console.log("磁盘空间申请成功,空间大小为:"+grantedBytes);
				},
				function(errorCode){
					console.log("磁盘空间申请失败");
				}
			);
			function createFile(){
				window.requestFileSystem(window.PERSISTENT,1024*1024,
					//successCallback回调方法
					function(fileSystem){
						console.log("fileSystem.name="+fileSystem.name);
					},
					//errorCallback回调方法
					function(error){
						console.log(error.name);
					}
				);
			}
		</script>
		</div><hr id="hBlock"> -->

		<!-- getFile创建文件 -->
		<div id="">
			<div id="">
				文件名:<input type="text" id="fileName" value="test.txt" /><br>
				文件大小:<input type="text" id="fileSize" value="1024" /><br>
				<input type="button" value="创建文件" onclick="createFile()" />
				<hr>
				<output id="result"></output>
			</div>
			<script type="text/javascript">
				window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
				navigator.webkitPersistentStorage.requestQuota(1024 * 1024,
					function(grantedBytes) {
    
						console.log("磁盘空间申请成功,空间大小为:" + grantedBytes);
					},
					function(errorCode) {
    
						console.log("磁盘空间申请失败");
					}
				);

				function createFile() {
    
					var fileSize = document.getElementById("fileSize").value;
					window.requestFileSystem(window.PERSISTENT, fileSize, successCallback, errorCallback);
				}

				function successCallback(fileSystem) {
    
					var fileName = document.getElementById("fileName").value;
					fileSystem.root.getFile(fileName, {
    
							create: true
						},
						function(fileEntry) {
    
							var text = "完整路径:" + fileEntry.fullPath + "<br/>文件名:" + fileEntry.name;
							document.getElementById("result").innerHTML = text;
						},
						function(error) {
    
							console.log(error.name);
							switch (error.name) {
    
								case &
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

is_Del

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值