vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Friday, February 4, 2011

Upload a File in ASP.NET using VB coding



hi..
In this article you are going to see about to upload file in asp.net

Folloe the client side coding......
DESIGN SIDE

Normally if you want to upload a file you have to add FILEUPLOAD its present in the toolbox

then you have to add one button rename it submit
Follow server side
SERVER SIDE:

In server side you have to add the following code in the submit button event ....

Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles 

 Try

            Dim filetype As String
            Dim file As String
            Dim filepath As String
            Dim uploadpath As String
          
            file = FileUpload1.PostedFile.FileName

            filepath = Path.GetFileName(file)
            uploadpath = "d:\vijay\"

                FileUpload1.PostedFile.SaveAs(Path.Combine(uploadpath, file))
                Response.Write("upload succesfully")
           
            

        Catch ex As Exception
            Response.Write(ex.Message)


        End Try
End Sub

Normally when upload one issue araise ,How to check the upload file name extension..this one was the big problem for developer....
For checking extension in VB present one function i.e CONTENT TYPE....
By using this function you have check the file extension coding..

In this exanble i checked the gif image file ...


Try

            Dim filetype As String
            Dim file As String
            Dim filepath As String
            Dim uploadpath As String
            filetype = FileUpload1.PostedFile.ContentType
            file = FileUpload1.PostedFile.FileName

            filepath = Path.GetFileName(file)
            uploadpath = "d:\vijay\"
            If filetype = "image/gif" Then

                FileUpload1.PostedFile.SaveAs(Path.Combine(uploadpath, file))
                Response.Write("upload succesfully")
            Else
                Response.Write("its not gif file")
            End If

        Catch ex As Exception
            Response.Write(ex.Message)


        End Try
By using this method you can easily check file type at the time of upload.....

i hope this article helpful to uu.....

plz post your valuable comments here..............

0 comments:

Post a Comment