ASP.NET 如何等待文件上传/发布?
我有用 VB 编写的 ASP.NET Intranet 应用程序。它从用户处获取文件,然后根据几种不同的情况,它可能会创建该文件的一些副本并移动原始文件。
不幸的是,我遇到了一个出现此错误的情况:
Exception Details: System.IO.IOException: The process cannot access the file
'\\some\dir\D09_03_5_180_0.000-6.788.png' because it is being used by
another process.
这是由 My.Computer.FileSystem.CopyFile
引发的。这很好,它被另一个进程使用 - 它可能仍在从用户那里保存/下载或尝试在另一个线程(?)正在复制时进行复制,我并不真正关心这一点,我想知道的是:
是有什么方法可以告诉 VB 等待复制(也移动)文件,直到文件不再使用?
谢谢
I've got ASP.NET intranet application written in VB. It gets a file from the user, and then depending on a few different cases it may create a few copies of the file as well as move the original.
Unfortunately I've come across a case where I get this error:
Exception Details: System.IO.IOException: The process cannot access the file
'\\some\dir\D09_03_5_180_0.000-6.788.png' because it is being used by
another process.
Which is thrown by My.Computer.FileSystem.CopyFile
. And that's fine that it's being used by another process - it may still be saving/downloading from the user or trying to copy while another thread(?) is copying, I don't really care about that, what I want to know:
Is there any way that I can tell VB to wait to copy (also move) the file until the file is no longer in use?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
测试文件是否正在使用并执行您需要执行的操作。
Public Sub WriteLogFile(ByVal pText As String, ByVal psPath As String, ByVal psName As String)
End Sub
公共函数 CheckIfFileIsInUse(ByVal sFile As String) As Boolean
如果 System.IO.File.Exists(sFile) 那么
尝试
Dim F As Short = FreeFile()
文件打开(F,sFile,OpenMode.Append,OpenAccess.Write,OpenShare.Shared)
文件关闭(F)
抓住
返回真
结束尝试
结束如果
结束功能
Test if the file is in use and the do what you need to do.
Public Sub WriteLogFile(ByVal pText As String, ByVal psPath As String, ByVal psName As String)
End Sub
Public Function CheckIfFileIsInUse(ByVal sFile As String) As Boolean
If System.IO.File.Exists(sFile) Then
Try
Dim F As Short = FreeFile()
FileOpen(F, sFile, OpenMode.Append, OpenAccess.Write, OpenShare.Shared)
FileClose(F)
Catch
Return True
End Try
End If
End Function
嗯……不直接。
大多数实现正在做的是在很短的时间内(几秒钟)重试复制文件,
如果您想制作一个漂亮的 UI,您可以通过 Ajax 检查复制过程是否顺利。
Hmm... not directly.
What most implementations are doing, is making a retry of copying the file, with a small timeframe (some seconds)
if you want to make a nice UI, you check via Ajax, if the copying process went well.
好吧,事实证明等待在这种情况下不起作用:
当尝试复制文件时,您不能将文件从一个位置复制到同一位置,否则会抛出错误(显然)。 VB 并不只是假装复制文件,而是实际上尝试复制文件并失败,因为复制操作试图复制到要复制的文件(至少使用
overwrite:=True
)。哎呀!
Well, it turns out that waiting would not work in this case:
When trying to copy a file you cannot copy a file from one location to the same location or it will throw an error (apparently). Rather than just pretending to copy the file, VB actually tries to copy the file and fails because the copy operation is trying to copy to the file it's copying from (with
overwrite:=True
at least).Whoops!