【发布时间】:2016-10-01 16:47:01
【问题描述】:
如果使用 type=file 的输入 html 元素选择文件,则 hta 程序无法删除该文件。
此 MVCE 工作但不使用文件对话框 - 您必须手动输入文件名:
<html>
<HEAD>
<SCRIPT Language="VBScript">
Sub Process
Set x = CreateObject("Scripting.FileSystemObject")
MsgBox "this will actually delete "& INIFile.Value
x.DeleteFile INIFile.Value
MsgBox "see? "& INIFile.Value &" is gone"
Set x = Nothing
End Sub
</SCRIPT>
</HEAD>
<body id='body'>
<input type="text" name="INIFile" >
<input type="button" value="Go!" onClick="Process" >
</body>
</html>
但是这个 MVCE 不起作用——文件没有被删除;只是推迟到程序退出:
<html>
<HEAD>
<SCRIPT Language="VBScript">
Sub Process
Set x = CreateObject("Scripting.FileSystemObject")
MsgBox "try to manually delete "& INIFile.Value &" (and then undo it)"
x.DeleteFile INIFile.Value
MsgBox "now try to delete file "& INIFile.Value &" (now it can't be deleted until the app is closed)"
Set x = Nothing
End Sub
</SCRIPT>
</HEAD>
<body id='body'>
<input type="file" name="INIFile" >
<input type="button" value="Go!" onClick="Process" >
</body>
</html>
不知何故,使用文件类型输入 html 元素使得可以从程序外部手动删除文件,直到调用 DeleteFile 函数。 DeleteFile 函数实际上并没有删除文件——它只是推迟删除,直到 hta 程序退出——此时文件最终会自行删除。
我需要在程序仍在运行时删除文件。有什么方法可以在 hta 文件中使用文件类型输入 html 元素,并且在 hta 程序运行时仍然删除该文件?
编辑
我的实际用例!在尝试生成可用的 MVCE 时,我没有意识到会找到不符合我的特定要求的解决方案。
我删除文件的原因是我可以用其他东西替换它,所以我需要文件在函数结束之前消失。 Call window.location.reload() 绝对有效,但文件在函数结束时消失。
我实际上想做的是这样的:
<HTML>
<HEAD>
<SCRIPT Language="VBScript">
Sub Process
Dim file: file = INIFile.Value
Call window.location.reload()
'backup the file to tempfile.tmp
'Now edit tempfile.tmp with all the changes and preview it
'then ask the user whether they are happy with the changes
'delete the original file
'and put the tempfile.tmp in its place
Dim x: Set x = CreateObject("Scripting.FileSystemObject")
x.CopyFile file,"tempfile.tmp"
x.DeleteFile file
MsgBox "why is "& file &" still there?"
x.MoveFile "tempfile.tmp",file ' this produces "file already exists"
Set x = Nothing
End Sub
</SCRIPT>
</HEAD>
<BODY id='body'>
<INPUT type="file" name="INIFile" onChange="Process">
</BODY>
</HTML>
【问题讨论】:
-
如果将
INIFile.Value设置为变量,然后在调用x.DeleteFile之前将INIFile.Value设置为"",会发生什么情况?文件输入 HTML 元素是为通过表单上传文件而设计的,它存储文件的句柄是有意义的。如果您将value属性设置为空,想知道它是否会继续持有它。
标签: vbscript delete-file hta html-input