Link to home
Start Free TrialLog in
Avatar of comt00006
comt00006

asked on

File uploading, file used by another process.

Hi there.

I am currently using an HTML FileField to upload files to my server. I get an error for one file saying that the file is being used by another process. Here is the "Error.ToString()"

Error Msg Start
---------------------------------------------------------------------------
ex.tostring      "System.IO.IOException: The process cannot access the file "C:\Packages\DataLoader\Data Files\Oracle.DataAccess.dll" because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String str)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode)
   at System.Web.HttpPostedFile.SaveAs(String filename)
   at Web_Compiller.mainDownload.cmdUpload_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\Web_Compiller\mainDownload.aspx.vb:line 534"      String

-----------------------------------------------------------------------------
Error Msg End

Is there a way of going around this. I have tried multiple files and this is the only one that gives me this error. If anyone knows the answer to my question I would really apreciate some help.

Thanks in advance.
Avatar of Volkan Vardar
Volkan Vardar

you can get the file in readonly mode
Avatar of comt00006

ASKER

How to do so???
Where in this article do you see how to make a file read-only?

START OF ARTICLE
---------------------------------------------------------------------------------------------------
Uploading a file on the server
Uploading a file from the client to the server is very straightforward with ASP.NET. Just use the <input type="file" runat="server"> tag and its wrapper class HtmlInputFile. Here's how you define the control on the page:

<form runat="server" enctype="multipart/form-data">
  Select a file to upload:<br>
  <input type="file" runat="server" ID="Uploader">
  <input type="button" runat="server" ID="Upload" value="Upload"
    OnServerClick="Upload_Click">
</form>

And here's the code you write in the code-behind file (or in the page's <script> section) to handle the button's Click event:

Sub Upload_Click(ByVal sender As Object, ByVal e As EventArgs)
    If Not Uploader.PostedFile Is Nothing Then
        Try
            ' retrieve the destination dir path
            Dim destDir As String = Server.MapPath("./Upload")
            ' extract the filename part from the full path of the posted file
            Dim fileName As String = System.IO.Path.GetFileName _
                (Uploader.PostedFile.FileName)
            ' combine the destination directory with the filename
            Dim destPath As String = System.IO.Path.Combine(destDir, fileName)
            ' save the file on the server
            Uploader.PostedFile.SaveAs(destPath)
            Response.Write("Thanks for submitting your file")
        Catch exc As Exception
            Response.Write(exc.Message)
        End Try
End Sub

Note that in order to handle the uploading of a file, the form must have the enctype attribute set to multipart/form-data, as you see above. Also note that by default the maximum size of the uploaded file is 4MB. If you try to upload a bigger size you'll get a runtime error. You may want to increase this value, for example because you have ZIP, AVI or MP3 files bigger than 4MB. To do so, set the maxRequestLength attribute of the <httpRuntime> setting in the application's web.config file. The size is specified in KB, so <httpRuntime maxRequestLength="8192" /> sets the maximum file size on 8MB.
---------------------------------------------------------------------------------------------------
END OF ARTICLE

That is how I have it set up in my page. The problem is when I try to upload a certain file "Oracle.DataAccess.dll" I get the error:

Uploading Error System.IO.IOException: The process cannot access the file "C:\Packages\DataLoader\Data Files\Oracle.DataAccess.dll" because it is being used by another process.

Is there a way around this?
I got the fix for it.

Here we go.

START CODING SNIPET
-----------------------------------------------------------------------------------
Private Sub WaitForExclusiveAccess(ByVal strFileName As String)

        Dim intIntervalCounter As Integer = 0

        ' This will keep trying to access the wanted file until
        ' it is finally accessible to the upload control
        While True

            Try

                ' Add to the interval counter
                intIntervalCounter += 1

                If (intIntervalCounter = 10) Then
                    ' Looping endlessly so we want to exit
                    Exit Sub
                End If

                Dim oFileStream As New FileStream(strFileName, FileMode.Append, FileAccess.Write, FileShare.None)
                oFileStream.Close()
                Exit Sub

            Catch ex As Exception
                Thread.Sleep(1000)
            End Try

        End While

    End Sub
-----------------------------------------------------------------------------------
END CODING SNIPET

Thanks for the replies
ASKER CERTIFIED SOLUTION
Avatar of GhostMod
GhostMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial