Connecting Tech Pros Worldwide Forums | Help | Site Map

How to upload an image in asp.net

Varun_789
Guest
 
Posts: n/a
#1: Nov 17 '05
hello friends,
i am developing an application in which i want to
upload an image from clients hard drive(local hard drive)
to the server.

i mean the application should provide the user with the
the faliclity to click on LoadPic button to open a
Open File Dialog on his/her system, then allows the user
to browse to the actually image and once the user is done,
by clicking on Send button, sends the image to the server
database.

i have SQL Server 2000 as RDBMS.

how can i accomplish this is ASP.NET ?


Steve C. Orr, MCSD
Guest
 
Posts: n/a
#2: Nov 17 '05

re: How to upload an image in asp.net


Drag a file field control from the HTML section of your toolbar onto your form.
Right click on it and make sure it's set to run as a server control.
Also drag a button onto your control to begin the upload.
Here's the code that goes under the button. This code will work to upload any file type, not just images.

Private Sub btnAttach_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAttach.Click

Dim iLength As Integer = _ CType(File1.PostedFile.InputStream.Length, Integer)

If iLength = 0 Then Exit Sub 'not a valid file

Dim sContentType As String = File1.PostedFile.ContentType

Dim sFileName As String, i As Integer

Dim bytContent As Byte()

ReDim bytContent(iLength) 'byte array, set to file size



'strip the path off the filename

i = InStrRev(File1.PostedFile.FileName.Trim, "\")

If i = 0 Then

sFileName = File1.PostedFile.FileName.Trim

Else

sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)

End If



Try

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)

With cmdInsertAttachment

.Parameters("@FileName").Value = sFileName

.Parameters("@FileSize").Value = iLength

.Parameters("@FileData").Value = bytContent

.Parameters("@ContentType").Value = sContentType

.ExecuteNonQuery()

End With

Catch ex As Exception

'Handle your database error here

dbConn.Close()

End Try

Response.Redirect(Request.Url.ToString) 'Refresh page

End Sub


Here's the SQL Query that the above code uses:
INSERT INTO tblAttachments

(FileName, FileSize, FileData, ContentType)

VALUES (@FileName, @FileSize, @FileData, @ContentType)



Here's more info:
http://www.aspnetpro.com/features/20...200307so_f.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net




"Varun_789" <varun_789@yahoo.com> wrote in message news:04d301c346d6$7a48c180$a001280a@phx.gbl...[color=blue]
> hello friends,
> i am developing an application in which i want to
> upload an image from clients hard drive(local hard drive)
> to the server.
>
> i mean the application should provide the user with the
> the faliclity to click on LoadPic button to open a
> Open File Dialog on his/her system, then allows the user
> to browse to the actually image and once the user is done,
> by clicking on Send button, sends the image to the server
> database.
>
> i have SQL Server 2000 as RDBMS.
>
> how can i accomplish this is ASP.NET ?
>[/color]
Microsoft
Guest
 
Posts: n/a
#3: Nov 17 '05

re: How to upload an image in asp.net


Is it possible to make this work for uploading folders as well?

Michael Murray
InterWorks Software, Inc.

"Steve C. Orr, MCSD" <Steve@Orr.net> wrote in message news:upNiANxRDHA.1804@TK2MSFTNGP11.phx.gbl...
Drag a file field control from the HTML section of your toolbar onto your form.
Right click on it and make sure it's set to run as a server control.
Also drag a button onto your control to begin the upload.
Here's the code that goes under the button. This code will work to upload any file type, not just images.

Private Sub btnAttach_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAttach.Click

Dim iLength As Integer = _ CType(File1.PostedFile.InputStream.Length, Integer)

If iLength = 0 Then Exit Sub 'not a valid file

Dim sContentType As String = File1.PostedFile.ContentType

Dim sFileName As String, i As Integer

Dim bytContent As Byte()

ReDim bytContent(iLength) 'byte array, set to file size



'strip the path off the filename

i = InStrRev(File1.PostedFile.FileName.Trim, "\")

If i = 0 Then

sFileName = File1.PostedFile.FileName.Trim

Else

sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)

End If



Try

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)

With cmdInsertAttachment

.Parameters("@FileName").Value = sFileName

.Parameters("@FileSize").Value = iLength

.Parameters("@FileData").Value = bytContent

.Parameters("@ContentType").Value = sContentType

.ExecuteNonQuery()

End With

Catch ex As Exception

'Handle your database error here

dbConn.Close()

End Try

Response.Redirect(Request.Url.ToString) 'Refresh page

End Sub


Here's the SQL Query that the above code uses:
INSERT INTO tblAttachments

(FileName, FileSize, FileData, ContentType)

VALUES (@FileName, @FileSize, @FileData, @ContentType)



Here's more info:
http://www.aspnetpro.com/features/20...200307so_f.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net




"Varun_789" <varun_789@yahoo.com> wrote in message news:04d301c346d6$7a48c180$a001280a@phx.gbl...[color=blue]
> hello friends,
> i am developing an application in which i want to
> upload an image from clients hard drive(local hard drive)
> to the server.
>
> i mean the application should provide the user with the
> the faliclity to click on LoadPic button to open a
> Open File Dialog on his/her system, then allows the user
> to browse to the actually image and once the user is done,
> by clicking on Send button, sends the image to the server
> database.
>
> i have SQL Server 2000 as RDBMS.
>
> how can i accomplish this is ASP.NET ?
> [/color]

Alex Munk
Guest
 
Posts: n/a
#4: Nov 17 '05

re: How to upload an image in asp.net


I followed the instructions but I am having a problem when the following code is executed:
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer)
I debugged the code and I found out that
File1.Value = "C:\Documents and Settings\Owner\My Documents\Word\Test File.doc"
File1.PostedFile = nothing

The error message is "System.NullReferenceException: Object reference not set to an instance of an object". Am I correct in assuming that this is because File1.PostedFile has a value of nothing? What might I do to corret this problem.

Best,
Alex

"Steve C. Orr, MCSD" <Steve@Orr.net> wrote in message news:upNiANxRDHA.1804@TK2MSFTNGP11.phx.gbl...
Drag a file field control from the HTML section of your toolbar onto your form.
Right click on it and make sure it's set to run as a server control.
Also drag a button onto your control to begin the upload.
Here's the code that goes under the button. This code will work to upload any file type, not just images.

Private Sub btnAttach_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAttach.Click

Dim iLength As Integer = _ CType(File1.PostedFile.InputStream.Length, Integer)

If iLength = 0 Then Exit Sub 'not a valid file

Dim sContentType As String = File1.PostedFile.ContentType

Dim sFileName As String, i As Integer

Dim bytContent As Byte()

ReDim bytContent(iLength) 'byte array, set to file size



'strip the path off the filename

i = InStrRev(File1.PostedFile.FileName.Trim, "\")

If i = 0 Then

sFileName = File1.PostedFile.FileName.Trim

Else

sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)

End If



Try

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)

With cmdInsertAttachment

.Parameters("@FileName").Value = sFileName

.Parameters("@FileSize").Value = iLength

.Parameters("@FileData").Value = bytContent

.Parameters("@ContentType").Value = sContentType

.ExecuteNonQuery()

End With

Catch ex As Exception

'Handle your database error here

dbConn.Close()

End Try

Response.Redirect(Request.Url.ToString) 'Refresh page

End Sub


Here's the SQL Query that the above code uses:
INSERT INTO tblAttachments

(FileName, FileSize, FileData, ContentType)

VALUES (@FileName, @FileSize, @FileData, @ContentType)



Here's more info:
http://www.aspnetpro.com/features/20...200307so_f.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net




"Varun_789" <varun_789@yahoo.com> wrote in message news:04d301c346d6$7a48c180$a001280a@phx.gbl...[color=blue]
> hello friends,
> i am developing an application in which i want to
> upload an image from clients hard drive(local hard drive)
> to the server.
>
> i mean the application should provide the user with the
> the faliclity to click on LoadPic button to open a
> Open File Dialog on his/her system, then allows the user
> to browse to the actually image and once the user is done,
> by clicking on Send button, sends the image to the server
> database.
>
> i have SQL Server 2000 as RDBMS.
>
> how can i accomplish this is ASP.NET ?
> [/color]

Alex Munk
Guest
 
Posts: n/a
#5: Nov 17 '05

re: How to upload an image in asp.net


I followed the instructions but I am having a problem when the following code is executed:
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer)
I debugged the code and I found out that
File1.Value = "C:\Documents and Settings\Owner\My Documents\Word\Test File.doc"
File1.PostedFile = nothing

The error message is "System.NullReferenceException: Object reference not set to an instance of an object". Am I correct in assuming that this is because File1.PostedFile has a value of nothing? What might I do to corret this problem.

Best,
Alex

"Steve C. Orr, MCSD" <Steve@Orr.net> wrote in message news:upNiANxRDHA.1804@TK2MSFTNGP11.phx.gbl...
Drag a file field control from the HTML section of your toolbar onto your form.
Right click on it and make sure it's set to run as a server control.
Also drag a button onto your control to begin the upload.
Here's the code that goes under the button. This code will work to upload any file type, not just images.

Private Sub btnAttach_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAttach.Click

Dim iLength As Integer = _ CType(File1.PostedFile.InputStream.Length, Integer)

If iLength = 0 Then Exit Sub 'not a valid file

Dim sContentType As String = File1.PostedFile.ContentType

Dim sFileName As String, i As Integer

Dim bytContent As Byte()

ReDim bytContent(iLength) 'byte array, set to file size



'strip the path off the filename

i = InStrRev(File1.PostedFile.FileName.Trim, "\")

If i = 0 Then

sFileName = File1.PostedFile.FileName.Trim

Else

sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)

End If



Try

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)

With cmdInsertAttachment

.Parameters("@FileName").Value = sFileName

.Parameters("@FileSize").Value = iLength

.Parameters("@FileData").Value = bytContent

.Parameters("@ContentType").Value = sContentType

.ExecuteNonQuery()

End With

Catch ex As Exception

'Handle your database error here

dbConn.Close()

End Try

Response.Redirect(Request.Url.ToString) 'Refresh page

End Sub


Here's the SQL Query that the above code uses:
INSERT INTO tblAttachments

(FileName, FileSize, FileData, ContentType)

VALUES (@FileName, @FileSize, @FileData, @ContentType)



Here's more info:
http://www.aspnetpro.com/features/20...200307so_f.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net




"Varun_789" <varun_789@yahoo.com> wrote in message news:04d301c346d6$7a48c180$a001280a@phx.gbl...[color=blue]
> hello friends,
> i am developing an application in which i want to
> upload an image from clients hard drive(local hard drive)
> to the server.
>
> i mean the application should provide the user with the
> the faliclity to click on LoadPic button to open a
> Open File Dialog on his/her system, then allows the user
> to browse to the actually image and once the user is done,
> by clicking on Send button, sends the image to the server
> database.
>
> i have SQL Server 2000 as RDBMS.
>
> how can i accomplish this is ASP.NET ?
> [/color]

Alex Munk
Guest
 
Posts: n/a
#6: Nov 17 '05

re: How to upload an image in asp.net


Hello All,
The problem with File1.PostedFile = Nothing can be resolved by adding adding "enctype" to your form like so:

<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
"Alex Munk" <amunk@hotmail.com> wrote in message news:27HPa.6413$1aB1.4759@news02.bloor.is.net.cabl e.rogers.com...
I followed the instructions but I am having a problem when the following code is executed:
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer)
I debugged the code and I found out that
File1.Value = "C:\Documents and Settings\Owner\My Documents\Word\Test File.doc"
File1.PostedFile = nothing

The error message is "System.NullReferenceException: Object reference not set to an instance of an object". Am I correct in assuming that this is because File1.PostedFile has a value of nothing? What might I do to corret this problem.

Best,
Alex

"Steve C. Orr, MCSD" <Steve@Orr.net> wrote in message news:upNiANxRDHA.1804@TK2MSFTNGP11.phx.gbl...
Drag a file field control from the HTML section of your toolbar onto your form.
Right click on it and make sure it's set to run as a server control.
Also drag a button onto your control to begin the upload.
Here's the code that goes under the button. This code will work to upload any file type, not just images.

Private Sub btnAttach_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAttach.Click

Dim iLength As Integer = _ CType(File1.PostedFile.InputStream.Length, Integer)

If iLength = 0 Then Exit Sub 'not a valid file

Dim sContentType As String = File1.PostedFile.ContentType

Dim sFileName As String, i As Integer

Dim bytContent As Byte()

ReDim bytContent(iLength) 'byte array, set to file size



'strip the path off the filename

i = InStrRev(File1.PostedFile.FileName.Trim, "\")

If i = 0 Then

sFileName = File1.PostedFile.FileName.Trim

Else

sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)

End If



Try

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)

With cmdInsertAttachment

.Parameters("@FileName").Value = sFileName

.Parameters("@FileSize").Value = iLength

.Parameters("@FileData").Value = bytContent

.Parameters("@ContentType").Value = sContentType

.ExecuteNonQuery()

End With

Catch ex As Exception

'Handle your database error here

dbConn.Close()

End Try

Response.Redirect(Request.Url.ToString) 'Refresh page

End Sub


Here's the SQL Query that the above code uses:
INSERT INTO tblAttachments

(FileName, FileSize, FileData, ContentType)

VALUES (@FileName, @FileSize, @FileData, @ContentType)



Here's more info:
http://www.aspnetpro.com/features/20...200307so_f.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net




"Varun_789" <varun_789@yahoo.com> wrote in message news:04d301c346d6$7a48c180$a001280a@phx.gbl...[color=blue]
> hello friends,
> i am developing an application in which i want to
> upload an image from clients hard drive(local hard drive)
> to the server.
>
> i mean the application should provide the user with the
> the faliclity to click on LoadPic button to open a
> Open File Dialog on his/her system, then allows the user
> to browse to the actually image and once the user is done,
> by clicking on Send button, sends the image to the server
> database.
>
> i have SQL Server 2000 as RDBMS.
>
> how can i accomplish this is ASP.NET ?
> [/color]

Alex Munk
Guest
 
Posts: n/a
#7: Nov 17 '05

re: How to upload an image in asp.net


Hello All,
The problem with File1.PostedFile = Nothing can be resolved by adding adding "enctype" to your form like so:

<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
"Alex Munk" <amunk@hotmail.com> wrote in message news:27HPa.6413$1aB1.4759@news02.bloor.is.net.cabl e.rogers.com...
I followed the instructions but I am having a problem when the following code is executed:
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer)
I debugged the code and I found out that
File1.Value = "C:\Documents and Settings\Owner\My Documents\Word\Test File.doc"
File1.PostedFile = nothing

The error message is "System.NullReferenceException: Object reference not set to an instance of an object". Am I correct in assuming that this is because File1.PostedFile has a value of nothing? What might I do to corret this problem.

Best,
Alex

"Steve C. Orr, MCSD" <Steve@Orr.net> wrote in message news:upNiANxRDHA.1804@TK2MSFTNGP11.phx.gbl...
Drag a file field control from the HTML section of your toolbar onto your form.
Right click on it and make sure it's set to run as a server control.
Also drag a button onto your control to begin the upload.
Here's the code that goes under the button. This code will work to upload any file type, not just images.

Private Sub btnAttach_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAttach.Click

Dim iLength As Integer = _ CType(File1.PostedFile.InputStream.Length, Integer)

If iLength = 0 Then Exit Sub 'not a valid file

Dim sContentType As String = File1.PostedFile.ContentType

Dim sFileName As String, i As Integer

Dim bytContent As Byte()

ReDim bytContent(iLength) 'byte array, set to file size



'strip the path off the filename

i = InStrRev(File1.PostedFile.FileName.Trim, "\")

If i = 0 Then

sFileName = File1.PostedFile.FileName.Trim

Else

sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)

End If



Try

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)

With cmdInsertAttachment

.Parameters("@FileName").Value = sFileName

.Parameters("@FileSize").Value = iLength

.Parameters("@FileData").Value = bytContent

.Parameters("@ContentType").Value = sContentType

.ExecuteNonQuery()

End With

Catch ex As Exception

'Handle your database error here

dbConn.Close()

End Try

Response.Redirect(Request.Url.ToString) 'Refresh page

End Sub


Here's the SQL Query that the above code uses:
INSERT INTO tblAttachments

(FileName, FileSize, FileData, ContentType)

VALUES (@FileName, @FileSize, @FileData, @ContentType)



Here's more info:
http://www.aspnetpro.com/features/20...200307so_f.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net




"Varun_789" <varun_789@yahoo.com> wrote in message news:04d301c346d6$7a48c180$a001280a@phx.gbl...[color=blue]
> hello friends,
> i am developing an application in which i want to
> upload an image from clients hard drive(local hard drive)
> to the server.
>
> i mean the application should provide the user with the
> the faliclity to click on LoadPic button to open a
> Open File Dialog on his/her system, then allows the user
> to browse to the actually image and once the user is done,
> by clicking on Send button, sends the image to the server
> database.
>
> i have SQL Server 2000 as RDBMS.
>
> how can i accomplish this is ASP.NET ?
> [/color]

Closed Thread