Upload File

Hi All
I am trying to allow users of my site to upload images to the server the form
I am using is below

<form id=“Form1” method=“post” runat=“server”>
<P>
Browse to Picture<br>
<INPUT type=“file” id=“MyFile” runat=“server”>
<BR>
<br>
When you have selected your picture click upload<BR>
<asp:Button id=“btnUpLoad” runat=“server” Text=“Upload”></asp:Button></P>
<P>
<asp:Label id=“lblInfo” runat=“server”></asp:Label></P>
<a href=“MyProfile.aspx”>My Profile</a>
</form>

The code behind below

Private Sub btnUpLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpLoad.Click
If Not MyFile.PostedFile Is Nothing Then
Dim strFileName As String
Dim intFileSize As Int32

		strFileName = MyFile.PostedFile.FileName
		intFileSize = MyFile.PostedFile.ContentLength

		lblInfo.Text = strFileName & " " & "/" & " " & intFileSize

		MyFile.PostedFile.SaveAs("c:\\Inetpub\\wwwroot\\JtaForumMySql\\ProfileImages\\ProfilePic_User_" & Session("UserID") & ".jpg")

	End If

End Sub

The session is set during login

The problem is that sometimes (not always) I get the dreaded error

The process cannot access the file “c:\Inetpub\wwwroot\JtaForumMySql\ProfileImages\ProfilePic_User_6.jpg” because it is being used by another process.

Can anyone help me please

The problem is probably somewhere you are opening the image and not disposing of it correctly. Thus why you cannot overwrite it.

Instead of using the full path, user Server.MapPath(“ProfileImages/ProfilePic_User_” & Session(“UserID”) & “.jpg”)

Thanks for the swift response NightStalker you pointed me in right direction

Problem was another page where image was created using

Dim myImg As System.Drawing.Image = System.Drawing.Image.FromFile(myImgPath)

and was never closed

I created the image differently using

Dim fs As System.IO.FileStream = New System.IO.FileStream(myImgPath, IO.FileMode.Open, IO.FileAccess.Read)
Dim myImg As System.Drawing.Image = System.Drawing.Image.FromStream(fs)
Then closed with

fs.Close()
fs = Nothing

Which now works

Out of interest is there a way to dispose of the file created using
Dim myImg As System.Drawing.Image = System.Drawing.Image.FromFile(myImgPath)

Great. Glad you got it worted.

Yes. Just say
myImage.Dispose()
myImage = null;

OK thanks app working like a charm now