2014-12-23 102 views
1

我在<input type="file">正在鎖定ADODB.recordset的文件。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

今天我確實有這個問題。我周圍有通過將輸入文件的副本中的子文件夾,然後連接到與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 
+0

又見http://stackoverflow.com/questions/37583532/using-file-input-element -in-HTA-文件防止-刪除選擇的文件 –

相關問題