ASP.NET 파일 업로드 / 릴리스를 기다리는 방법은 무엇입니까?
-
26-09-2020 - |
문제
ASP.NET 인트라넷 응용 프로그램이 VB로 작성되었습니다.사용자의 파일을 가져 와서 몇 가지 다른 경우에 따라 파일의 몇 장의 복사본을 만들뿐만 아니라 원본을 이동할 수 있습니다.
불행히도이 오류가 발생하는 경우를 나타냅니다.
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
에 의해 던진
.그리고 그것은 다른 프로세스에서 사용되고있는 괜찮습니다. 사용자에서 저장 / 다운로드하거나 다른 스레드 (?)가 복사하는 동안 복사하려고 할 수 있습니다. 나는 그것에 대해 정말로 관심이 없습니다.
파일이 더 이상 사용되지 않을 때까지 파일을 복사 할 때까지 기다릴 수있는 방법이 있습니까?
감사합니다
해결책
파일이 사용 중이고 수행 해야하는 작업을 테스트하십시오.
Public Sub WriteLogFile (ByVal Ptext 문자열로 PsName, ByVal PSName 문자열로 ByVal PSName)
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로 문자열로) 부울로서 system.io.file.exs (sfile) 다음에있는 경우 시험 짧은 f= freefile () fileOpen (f, sfile, openmode.append, openaccess.write, openshare.shared) FileClose (f) 잡기 Return true TRY. If. 끝 기능
다른 팁
흠 ... 직접적으로 아닙니다. 대부분의 구현이 수행중인 경우 파일 복사를 재 시도하는 것, 작은 시간 (몇 초)
멋진 UI를 원한다면 복사 프로세스가 잘되면 AJAX를 통해 확인하십시오.
이 경우에 기다리지 않을 것이라는 점을 밝힙니다.
파일을 복사하려고 할 때 파일을 한 위치에서 동일한 위치로 복사하거나 오류 (분명히) 오류가 발생할 수 없습니다.파일을 복사하는 것처럼 VB가 실제로 복사 작업이 복사중인 파일로 복사하려는 파일에 복사하려고 시도합니다.
hops!