2010-07-13 18 views
-2

在下面的代码我怎样才能使它将文件加载到一个文本,其中textarea的路径是parent.a.frame_name1。 document.form_name1.textarea_name1HTML代码(任何专业有源-X或javascript帮我)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Language" content="en-us"> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>Local File I/O</title> 
<script type="text/javascript"> 
<!-- // Begin 
var ForReading = 1, 
    ForWriting = 2, 
    ForAppending = 8; 
var objFSO = new ActiveXObject("Scripting.FileSystemObject"); 

function checkText(fld, btn) { 
    btn.disabled = false; 
    fld.onkeypress = null; 
    return true; 
} 

function checkFile(obj, div, btn, btn2, fld) { 
    div.innerHTML = '<p><b>File:</b><br><b>Size:</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Last Modified:</b></p>'; 
    btn.disabled = true; 
    btn2.disabled = true; 
    fld.value = ''; 
    fld.onkeypress = new Function("return checkText(" + "document." + fld.form.name + "." + fld.name + "," + "document." + btn2.form.name + "." + btn2.name + ")"); 
    // 
    // if (obj.value.indexOf(":") != 1) { 
    // alert("Local file name must include a drive letter."); 
    // return false; 
    // } 
    var ary = obj.value.split("\\"); 
    if (ary.length < 2) { 
     alert("Local file name must include a path."); 
     return false; 
    } 
    if (!/(\.txt)$/i.test(obj.value)) { 
     alert("Local file name must include the '.txt' extension."); 
     return false; 
    } 
    // 
    try { 
     objFile = objFSO.GetFile(obj.value); 
    } catch (e) { 
     if (e.message != "File not found") { 
      alert(e.message); 
      return false; 
     } else { 
      try { 
       if (confirm(obj.value + "\n" + "does not exist. Click 'Ok' to create it.")) { 
        objFile = objFSO.CreateTextFile(obj.value); 
        objFile.Close(); 
        objFile = objFSO.GetFile(obj.value); 
       } else { 
        return false; 
       } 
      } catch (e) { 
       alert(e.message); 
       return false; 
      } 
     } 
    } 
    fileSpecs(div, btn); 
    objFile = null; 
    return true; 
} 

function fileSpecs(div, btn) { 
    if (objFile.Size > 0) { 
     btn.disabled = false; 
    } else { 
     btn.disabled = true; 
    } 
    var str = '<p>'; 
    str += '<b>File:</b> ' + objFile.Path + '<br>'; 
    str += '<b>Size:</b> '; 
    if (objFile.Size < 1024) { 
     str += objFile.Size + ' bytes'; 
    } else { 
     str += (objFile.Size/1024).toFixed(1) + ' Kbytes'; 
    } 
    str += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; 
    str += '<b>Last Modified:</b> ' + objFile.DateLastModified; 
    str += '</p>'; 
    div.innerHTML = str; 
} 

function loadFile(btn, obj, div, fld) { 
    objFile = objFSO.GetFile(obj.value); 
    objStream = objFile.OpenAsTextStream(ForReading); 
    fld.value = objStream.ReadAll(); 
    objStream.Close(); 
    objStream = null; 
    fileSpecs(div, btn); 
    objFile = null; 
    return true; 
} 

function saveFile(btn, obj, div, fld, btn2) { 
    btn.disabled = true; 
    objFile = objFSO.GetFile(obj.value); 
    objStream = objFile.OpenAsTextStream(ForWriting); 
    objStream.Write(fld.value); 
    objStream.Close(); 
    objStream = null; 
    objFile = objFSO.GetFile(obj.value); 
    fileSpecs(div, btn2); 
    objFile = null; 
    fld.value = ''; 
    fld.onkeypress = new Function("return checkText(" + "document." + fld.form.name + "." + fld.name + "," + "document." + btn.form.name + "." + btn.name + ")"); 
    return true; 
} 
// End --> 
</script> 
</head> 

<body> 
<center> 
<form name="myForm" onsubmit="return false;"> 
<table width="720"> 
<tr> 
<td colspan="4"> 
<div id="fileSpec"> 
<p><b>File:</b><br><b>Size:</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Last Modified:</b></p> 
</div> 
</td> 
</tr> 
<tr> 
<td colspan="3" width="580" align="center" valign="top"> 
<textarea rows="25" name="fileArea" cols="70" 
onkeypress="return checkText(this, btnSave);"></textarea> </td> 
<td rowspan="2" width="140" valign="top"><p>This is a simple demonstration of 
a browser-based local text file editor.&nbsp; Begin by clicking the <b>Browse</b> 
button to select an existing text file from your local hard drive.&nbsp; 
(In the Browse dialog, you may type in a new file name if 
desired.)&nbsp; The selected file information is then displayed at the 
top of the page.&nbsp; For an existing file, click the <b>Load</b> 
button next.&nbsp; After editing, click the <b>Save</b> button to 
complete the demonstration.</p></td> 
</tr> 
<tr> 
<td align="left"> 
<input type="file" name="fileName" size="50" 
onchange="return checkFile(this, document.getElementById('fileSpec'), btnLoad, btnSave, fileArea);"> </td> 
<td align="center"> 
<input type="button" name="btnLoad" value="Load" disabled 
onclick="return loadFile(this, fileName, document.getElementById('fileSpec'), fileArea);"> </td> 
<td align="center"> 
<input type="button" name="btnSave" value="Save" disabled 
onclick="return saveFile(this, fileName, document.getElementById('fileSpec'), fileArea, btnLoad);"> </td> 
</tr> 
</table> 
</form> 
</center> 

</body> 
</html> 

(也告诉我,无论何时何你在我的岗位已编辑,伤心人来了,我的编辑后)

+0

您的代码不完整。 您需要使用带扩展名的(隐藏)输入字段的函数checkFile,并且您需要fileSpec函数来为按钮执行某些操作(如启用它们)。当我加入空函数对于这些,文件加载 - 任何文本文件,包括HTML – mplungjan 2010-07-13 09:57:08

+0

对不起张贴残缺码 – subanki 2010-07-13 10:01:25

+0

在上面的代码中,我怎么可以让它将文件加载到一个文本,其中textarea的路径是parent.a.frame_name1.document .form_name1.textarea_name1 – subanki 2010-07-13 14:50:16

回答

0

更换线

if (!/(\.txt)$/i.test(obj.value)) { 
    alert("Local file name must include the '.txt' extension."); 
    return false; 
} 

if (!/(\.txt|\.htm|\.html)$/i.test(obj.value)) { 
    alert("Local file name must include the '.txt', '.htm' or '.html' extension."); 
    return false; 
} 

为了您的方便,它现在也需要htm文件。

+0

感谢名单FRN,我会尝试执行,并通知你 – subanki 2010-07-13 10:56:57

+0

AlbertVanHalen先生,代码工作现在比你FRND,你能告诉我你的真实姓名和形成whihc国家你是从..所以我可以在我的creidt页面包括乌尔名字.. PLZ这里 http://stackoverflow.com/questions/3236432/how-to-put-my-webpage张贴-所以-that-i-can-find-my-page-in-google-search – subanki 2010-07-13 11:14:07

+1

PS:你会发现在IE8中可能会失败,因为文件对象不再返回真实的文件路径,除非安全性降低。我猜的安全性是在这种情况下已经很低,因为你使用的文件系统对象来读取文件 – mplungjan 2010-07-13 11:49:18