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\Da ta Files\Oracle.DataAccess.dl l" because it is being used by another process.
at System.IO.__Error.WinIOErr or(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(Str ing filename)
at Web_Compiller.mainDownload .cmdUpload _Click(Obj ect sender, EventArgs e) in C:\Inetpub\wwwroot\Web_Com piller\mai nDownload. aspx.vb:li ne 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.
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\Da
at System.IO.__Error.WinIOErr
at System.IO.FileStream..ctor
at System.IO.FileStream..ctor
at System.Web.HttpPostedFile.
at Web_Compiller.mainDownload
--------------------------
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.
you can get the file in readonly mode
ASKER
How to do so???
ASKER
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-da ta">
Select a file to upload:<br>
<input type="file" runat="server" ID="Uploader">
<input type="button" runat="server" ID="Upload" value="Upload"
OnServerClick="Upload_Clic k">
</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.FileN ame)
' combine the destination directory with the filename
Dim destPath As String = System.IO.Path.Combine(des tDir, 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\Da ta Files\Oracle.DataAccess.dl l" because it is being used by another process.
Is there a way around this?
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-da
Select a file to upload:<br>
<input type="file" runat="server" ID="Uploader">
<input type="button" runat="server" ID="Upload" value="Upload"
OnServerClick="Upload_Clic
</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.FileN
' combine the destination directory with the filename
Dim destPath As String = System.IO.Path.Combine(des
' save the file on the server
Uploader.PostedFile.SaveAs
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\Da
Is there a way around this?
ASKER
I got the fix for it.
Here we go.
START CODING SNIPET
-------------------------- ---------- ---------- ---------- ---------- ---------- -------
Private Sub WaitForExclusiveAccess(ByV al 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
Here we go.
START CODING SNIPET
--------------------------
Private Sub WaitForExclusiveAccess(ByV
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.