贴一段用javascript打开和保存本地文件的源码
2008-09-01 19:59:08 来源:WEB开发网核心提示:<object id="fileDialog" width="0px" height="0px" classid="clsid:F9043C85-F6F2-101A-A3C9-08002B2F49FB" codebase="
<object id="fileDialog" width="0px" height="0px" classid="clsid:F9043C85-F6F2-101A-A3C9-08002B2F49FB" codebase="http://activex.microsoft.com/controls/vb5/comdlg32.cab">
</object>
<textarea id=TxtBody style="background-color:#EEEEEE;width:100%;height:100">无标题</textarea>
<br>
<input type=button value=加载 >
<input type=button value=保存 >
<script>
function loadDoc()
{
fileDialog.CancelError=true;
try{
fileDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt";
fileDialog.ShowOpen();
var fso=new ActiveXObject("Scripting.FileSystemObject");
var reading=1;
var f=fso.OpenTextFile(fileDialog.filename,reading);
//window.confirm(f);
var r=f.ReadAll();
f.close();
TxtBody.value=r;
}catch(e){}
}
function saveDoc()
{
fileDialog.CancelError=true;
try{
fileDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt";
fileDialog.ShowSave();
var fso=new ActiveXObject("Scripting.FileSystemObject");
var f=fso.CreateTextFile(fileDialog.filename,true);
f.write(TxtBody.value);
f.Close();
}
catch(e){}
}
</script>
</body>
</object>
<textarea id=TxtBody style="background-color:#EEEEEE;width:100%;height:100">无标题</textarea>
<br>
<input type=button value=加载 >
<input type=button value=保存 >
<script>
function loadDoc()
{
fileDialog.CancelError=true;
try{
fileDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt";
fileDialog.ShowOpen();
var fso=new ActiveXObject("Scripting.FileSystemObject");
var reading=1;
var f=fso.OpenTextFile(fileDialog.filename,reading);
//window.confirm(f);
var r=f.ReadAll();
f.close();
TxtBody.value=r;
}catch(e){}
}
function saveDoc()
{
fileDialog.CancelError=true;
try{
fileDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt";
fileDialog.ShowSave();
var fso=new ActiveXObject("Scripting.FileSystemObject");
var f=fso.CreateTextFile(fileDialog.filename,true);
f.write(TxtBody.value);
f.Close();
}
catch(e){}
}
</script>
</body>
Tags:段用 javascript 打开
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接