Javascript 怎么创建File对象

JavaScript024

Javascript 怎么创建File对象,第1张

var filepath="D:\\Test\\文本1.txt"

var fso = new ActiveXObject("Scripting.FileSystemObject")

var file = fso.GetFile(filepath)

dispay("DateCreated:"+file.DateCreated)

dispay("DateLastAccessed:"+file.DateLastAccessed)

dispay("DateLastModified:"+file.DateLastModified)

dispay("Drive:"+file.Drive)

dispay("Name:"+file.Name)

dispay("ParentFolder:"+file.ParentFolder)

dispay("Path:"+file.Path)

dispay("ShortName:"+file.ShortName)

dispay("ShortPath:"+file.ShortPath)

dispay("Size:"+file.Size)

dispay("Type:"+file.Type)

function dispay(value){

document.write(value+"<br/>")

}

代码如下:

<script type="text/javascript">

//FX获取文件路径方法

function readFileFirefox(fileBrowser) {

try {

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")

}

catch (e) {

alert('无法访问本地文件,由于浏览器安全设置。为了克服这一点,请按照下列步骤操作:(1)在地址栏输入"about:config"(2) 右键点击并选择 New->Boolean(3) 输入"signed.applets.codebase_principal_support" (不含引号)作为一个新的首选项的名称(4) 点击OK并试着重新加载文件')

return

}

var fileName=fileBrowser.value//这一步就能得到客户端完整路径。下面的是否判断的太复杂,还有下面得到ie的也很复杂。

var file = Components.classes["@mozilla.org/file/local1"]

.createInstance(Components.interfaces.nsILocalFile)

try {

// Back slashes for windows

file.initWithPath( fileName.replace(/\//g, "\\\\") )

}

catch(e) {

if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e

alert("File '" + fileName + "' cannot be loaded: relative paths are not allowed. Please provide an absolute path to this file.")

return

}

if ( file.exists() == false ) {

alert("File '" + fileName + "' not found.")

return

}