[转]Loading, Editing, and Saving a Text File in HTML5 Using Javascript
The HTML and JavaScript code below makes use of some features of HTML5
(specifically the “Blob” object, the File API, and the “download” attribute of the “a” tag)
to allow the user to load, edit, and save a text file on their local computer.
As of this writing,
it works in both Chrome and Firefox browsers,
though sadly it requires a little bit of custom code for each.
<html>
<body> <table>
<tr><td>Text to Save:</td></tr>
<tr>
<td colspan="3">
<textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
</td>
</tr>
<tr>
<td>Filename to Save As:</td>
<td><input id="inputFileNameToSaveAs"></input></td>
<td><button onclick="saveTextAsFile()">Save Text to File</button></td>
</tr>
<tr>
<td>Select a File to Load:</td>
<td><input type="file" id="fileToLoad"></td>
<td><button onclick="loadFileAsText()">Load Selected File</button><td>
</tr>
</table> <script type='text/javascript'> function saveTextAsFile()
{
var textToWrite = document.getElementById("inputTextToSave").value;
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value; var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
} downloadLink.click();
} function destroyClickedElement(event)
{
document.body.removeChild(event.target);
} function loadFileAsText()
{
var fileToLoad = document.getElementById("fileToLoad").files[0]; var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("inputTextToSave").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
} </script> </body>
</html>
[转]Loading, Editing, and Saving a Text File in HTML5 Using Javascript的更多相关文章
- shell脚本执行时报"bad interpreter: Text file busy"的解决方法
在执行一个shell脚本时,遇到了“-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy”错误提示,如下所示: [or ...
- eclipse的使用-------Text File Encoding没有GBK选项的设置
eclipse的使用-------Text File Encoding没有GBK选项的设置 2013-12-25 09:48:06 标签:java myeclipse使用 有一个项目是使用GBK编码的 ...
- Writing Text File From A Tabular Block In Oracle Forms
The example given below for writing text file or CSV using Text_IO package from a tabular block in O ...
- create feature from text file
'''---------------------------------------------------------------------------------- Tool Name: Cre ...
- The 12th tip of DB Query Analyzer, powerful in text file process
MA Gen feng ( Guangdong Unitoll Services incorporated, Guangzhou 510300) Abstract It's very powerf ...
- 【转】shell脚本执行时报"bad interpreter: Text file busy"的解决方法
1)问题现象: 在ubuntu下执行以下脚本( while_count),报错: -bash: ./while_count: /bin/bash: bad interpreter: Text file ...
- New text file line delimiter
Window -> Preferences -> General -> Workspace : Text file encoding :Default : 选择此项将设定文件为系统默 ...
- [转]How to Import a Text File into SQL Server 2012
Importing a using the OpenRowSet() Function The OPENROWSET bulk row set provider is accessed by call ...
- unity, read text file
using System.IO; //test read txt //Resources.Load(...) loads an asset stored at path in a Res ...
随机推荐
- Oracle 系统表大全
数据字典dict总是属于Oracle用户sys的. 1.用户: select username from dba_users; 改口令 alter user spgroup identified by ...
- Windows下启动redis错误1067:进程意外中止
已解决: 在redis-64.3.0.503文件夹下新建一个空文件夹,命名为logs,如下图所示: 最后成功了 开启服务:redis-server --service-start
- nfs搭建和挂载
1.搭建server a.创建共享目录 mkdir /nfs1 b.vim /etc/sysconfig/nfs 固定端口 c.vim /etc/export /nfs1 192.168.10 ...
- w2008 R2 401 - 未授权: 由于凭据无效,访问被拒绝。
1.打开服务器的"IIS信息服务管理器"-->选择你发布的网站-->选择功能视图中的"身份验证"-->右键匿名身份验证,选择"编辑& ...
- VB.NET提取TXT文档指定内容
今天有浏览论坛时,又看见一篇是读取TXT文本文件的论题.Insus.NET也想以自己的想法来实现,并分享于此. 文本文件是比较复杂,获取数据也是一些文本行中取其中一部分.为了能够取到较精准的数据,In ...
- git rm 与 git reset
https://www.cnblogs.com/sunshine-xin/articles/3521481.html 1. git rm --cached file will remove the f ...
- StampedLock原理
原文链接:https://blog.csdn.net/sunhaoning/article/details/68924625 StamppedLock是Java 8中引入的一种新的锁机制.读写锁虽然分 ...
- Buy or Build UVA - 1151 Kruskal+枚举
题意: 大概意思是有 n 个点,现在有 q 个方案 ,第 i 个方案耗费为 ci ,使 Ni 个点联通 ,当然也可以直接使两点联通 ,现求最小生成树的代价. 两点直接联通的代价是欧几里得距离的平方: ...
- Super-Resolution Restoration of MISR Images Using the UCL MAGiGAN System 超分辨率恢复
作者是伦敦大学学院Mullard空间科学实验室成像组,之前做过对火星图像的分辨率增强. 文章用了许多的图像处理方法获得特征和高分辨率的中间结果,最后用一个生产对抗网络获得更好的高分辨率结果. 用的数据 ...
- java 的开发工具IDEA使用 中Debug 调试工具使用
IDEA JetBrains(这个公司的名称翻译过来就是“大脑喷射?”) 公司的宣传语是这么说的:Develop with pleasure!(带着快乐开发!) 话不多说 开始debug 1, ...