输入类型="文件"锁定ADODB.Recordset的文件

时间:2014-12-23 17:10:49

标签: vbscript hta

我正在使用<input type="file">锁定ADODB.recordset的文件。

如果我对文件路径进行硬编码,代码运行时没有问题,但是当我使用输入类型的文件浏览并选择硬编码文件时,它会锁定文件,我无法再通过记录集访问它。

我已经尝试过所有我能想到的事情而没有任何成功。我知道它是输入浏览功能的结果,因为如果我在同一目录中选择另一个文件或单击过程按钮而不浏览代码,则应该按原样运行。

以下是相关的html和vbscript。有没有人对如何解决这个问题有任何想法?

<html>
<head>
<title>Employee Upload</title>
<HTA:APPLICATION
  APPLICATIONNAME="Employee Upload"
  ID="Employee Upload"
  VERSION="1.0"/> 

</head>

<body bgcolor="white">
<p id="heading" name="heading"><p>
<div id="container" name="container">
<span onClick="document.getElementById('myFile').click();" language="javascript" class="upload">
<button>Browse</button>
<input id="filename" type="text" disabled value="">
<input type="file" id="myFile" style="visibility:hidden;display:none;" onchange="document.getElementById('filename').value = this.value;document.getElementById('process').style.visibility = 'visible';" language="javascript">
</span>

<p>Click "Process File" once you have selected the file to upload the new hire data.</p>

<button id="process" name="process" onclick="loadFile()" style="/*visibility: hidden;*/">Process File</button>
</div>
<script language="vbscript">

Function loadFile()
On Error Resume Next
fileStr = document.all("filename").value
fileStr = "C:\Users\SeanW\Desktop\imports\NewHires.txt"
fileDir = Left(fileStr,InStrRev(fileStr,"\"))
filenameStr = Right(fileStr,Len(fileStr)-InStrRev(fileStr,"\"))

Set oConn = CreateObject("ADODB.Connection")
Set oRS = CreateObject("ADODB.Recordset")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & fileDir & ";" & _
          "Extended Properties=""text;HDR=YES;FMT=Delimited"""

oRS.Open "SELECT * FROM [" & filenameStr & "]", oConn, 3, 3, 1

If Err.Number <> 0 Then
MsgBox "Error Loading File: " & vbCrLf & vbCrLf & Err.Description,vbCritical,"File Load Error"
oConn.Close
oRS.Close
Set oConn = Nothing
Set oRs = Nothing
Err.Clear
Exit Function
else

Msgbox "File Loaded Successfully"
oConn.Close
oRS.Close
Set oConn = Nothing
Set oRs = Nothing

End If

End Function

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我今天遇到了这个问题。我通过在子文件夹中复制输入文件,然后使用ADODB.Connection

连接到该文件来解决这个问题。
dim txtfile: txtfile = document.getElementById("filename").Value
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
dim tablename: tablename = fso.GetFileName(txtfile)
' we'll create the folder as a subfolder to the current one
dim currentfolder: currentfolder = fso.GetAbsolutePathName(".")

' create new paths until we have a new one
dim newpath: newpath = fso.BuildPath(currentfolder, fso.GetTempName())
do while fso.folderExists(newpath)
    newpath = fso.BuildPath(currentfolder, fso.GetTempName())
loop

' create the folder & copy the input file
fso.createFolder newpath
fso.copyfile txtfile, fso.buildpath(newpath, tablename)

'connect and process
ado.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=" & newpath & ";" & _
      "Extended Properties=""text;HDR=YES;FMT=Delimited"""

ado.open

'... etc

' clear up the temp folder
fso.deleteFolder newpath, true