473,382 Members | 1,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to upload an image in asp.net

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 ?

Nov 17 '05 #1
6 12098
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" <va*******@yahoo.com> wrote in message news:04****************************@phx.gbl...
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 ?

Nov 17 '05 #2
Is it possible to make this work for uploading folders as well?

Michael Murray
InterWorks Software, Inc.

"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message news:up**************@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" <va*******@yahoo.com> wrote in message news:04****************************@phx.gbl...
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 ?


Nov 17 '05 #3
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" <St***@Orr.net> wrote in message news:up**************@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" <va*******@yahoo.com> wrote in message news:04****************************@phx.gbl...
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 ?


Nov 17 '05 #4
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" <St***@Orr.net> wrote in message news:up**************@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" <va*******@yahoo.com> wrote in message news:04****************************@phx.gbl...
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 ?


Nov 17 '05 #5
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" <am***@hotmail.com> wrote in message news:27******************@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" <St***@Orr.net> wrote in message news:up**************@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" <va*******@yahoo.com> wrote in message news:04****************************@phx.gbl...
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 ?


Nov 17 '05 #6
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" <am***@hotmail.com> wrote in message news:27******************@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" <St***@Orr.net> wrote in message news:up**************@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" <va*******@yahoo.com> wrote in message news:04****************************@phx.gbl...
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 ?


Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
by: SEMIH DEMIR | last post by:
Sitelerden birinde verilen yabancı kaynakli bir scriptti duzenledim yanlız birseyin içinden bir turlu cıkamadım işin aslı ilk defa persistin upload componentini kullanacam yanlız suanki haliyle...
4
by: Jim Michaels | last post by:
after a file upload, $_FILES is not populated but $_POST is. what's going on here? $_POST=C $_POST=C $_POST=C $_POST=C:\\www\\jimm\\images\\bg1.jpg $_FILES= $_FILES= $_FILES=
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
3
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
1
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
7
by: xx75vulcan | last post by:
Hi, I've got a PHP Upload Form that works great, unless that is, the image your uploading has been modified through a photo editing software. Example: if I upload the image straight from a...
7
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir =...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.