ASP.NET 如何等待文件上传/发布?

发布于 2024-09-10 23:34:02 字数 493 浏览 6 评论 0原文

我有用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所谓啦 2024-09-17 23:34:02

测试文件是否正在使用并执行您需要执行的操作。

Public Sub WriteLogFile(ByVal pText As String, ByVal psPath As String, ByVal psName As String)

  Dim strFullFileName As String
  Dim Writer As System.IO.StreamWriter
  Dim Fs As System.IO.FileStream

  Try
     Dim DirectoryHandler As New System.IO.DirectoryInfo(psPath)
     strFullFileName = psPath & "\" & psName & Date.Today.Month.ToString & "-" & Date.Today.Day.ToString & "-" & Date.Today.Year.ToString & ".txt"
     If Not DirectoryHandler.Exists() Then
        Try
           Monitor.Enter(fsLocker)
           DirectoryHandler.Create()
        Finally
           Monitor.Exit(fsLocker)
        End Try
     End If

     Try
           If CheckIfFileIsInUse(strFullFileName) = True Then
           Thread.Sleep(500) ' wait for .5 second
           WriteLogFile(pText, psPath, psName)
           If Not Fs Is Nothing Then Fs.Close()
           If Not Writer Is Nothing Then Writer.Close()
           Exit Sub
        End If
        Monitor.Enter(fsLocker)
        Fs = New System.IO.FileStream(strFullFileName, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.Write)
        Writer = New System.IO.StreamWriter(Fs)
        Writer.WriteLine(Date.Now.ToString & vbTab & "ProcessID: " & Process.GetCurrentProcess.Id.ToString() & vbTab & pText)
        Writer.Close()
        Fs.Close()
     Finally
        Monitor.Exit(fsLocker)
     End Try

  Catch ex As Exception

     Dim evtEMailLog As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
     evtEMailLog.Source = Process.GetCurrentProcess.ProcessName.ToString()
     evtEMailLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error)

  Finally
     If Not Fs Is Nothing Then Fs.Close()
     If Not Writer Is Nothing Then Writer.Close()
  End Try

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)

  Dim strFullFileName As String
  Dim Writer As System.IO.StreamWriter
  Dim Fs As System.IO.FileStream

  Try
     Dim DirectoryHandler As New System.IO.DirectoryInfo(psPath)
     strFullFileName = psPath & "\" & psName & Date.Today.Month.ToString & "-" & Date.Today.Day.ToString & "-" & Date.Today.Year.ToString & ".txt"
     If Not DirectoryHandler.Exists() Then
        Try
           Monitor.Enter(fsLocker)
           DirectoryHandler.Create()
        Finally
           Monitor.Exit(fsLocker)
        End Try
     End If

     Try
           If CheckIfFileIsInUse(strFullFileName) = True Then
           Thread.Sleep(500) ' wait for .5 second
           WriteLogFile(pText, psPath, psName)
           If Not Fs Is Nothing Then Fs.Close()
           If Not Writer Is Nothing Then Writer.Close()
           Exit Sub
        End If
        Monitor.Enter(fsLocker)
        Fs = New System.IO.FileStream(strFullFileName, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.Write)
        Writer = New System.IO.StreamWriter(Fs)
        Writer.WriteLine(Date.Now.ToString & vbTab & "ProcessID: " & Process.GetCurrentProcess.Id.ToString() & vbTab & pText)
        Writer.Close()
        Fs.Close()
     Finally
        Monitor.Exit(fsLocker)
     End Try

  Catch ex As Exception

     Dim evtEMailLog As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
     evtEMailLog.Source = Process.GetCurrentProcess.ProcessName.ToString()
     evtEMailLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error)

  Finally
     If Not Fs Is Nothing Then Fs.Close()
     If Not Writer Is Nothing Then Writer.Close()
  End Try

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

摘星┃星的人 2024-09-17 23:34:02

嗯……不直接。
大多数实现正在做的是在很短的时间内(几秒钟)重试复制文件,

如果您想制作一个漂亮的 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.

顾冷 2024-09-17 23:34:02

好吧,事实证明等待在这种情况下不起作用:

当尝试复制文件时,您不能将文件从一个位置复制到同一位置,否则会抛出错误(显然)。 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文