Show and Upload an image

S

sameer

Hi, i am new to asp.net.
From the web page I want to give the user option to browse on the local file
system and select an image file, when the user selects this image file, i
want to show the image to the user in the asp page. Then when the use clicks
on submit, i want to upload this image along with the other text fields on
the same page( which the user will enter) . In asp 3 days there used to be
3rd prty controls to do this, how is it possible in ASP.net ?

if showing the image before uploading become a big deal , i dont mind
skipping that step but would definetly want to upload the image.

thanks
Sameer
 
G

Gold

Hi,

it's simle, but there is no web form file control, so you have to use the
file control available under HTML controls. and set the runat property of
that control to server.
In your button event use the following statement to get the uploaded file
details
(assume the control is named as fileCntrl)

fileCntrl.PostedFile.FileName -- gives you the selected file name
*place a image control in your page and assign this filename to the
src
property of the control at run time, to show the image before
uploading

fileCntrl.PostedFile.FileStream -- gives you the stream obj ref, so u can
read the contents of the file, and store it into a DB or folder, whatever way
you want.


Regards,
:) Thangam
 
S

sameer

thanks Gold, will try it .
Sameer

Gold said:
Hi,

it's simle, but there is no web form file control, so you have to use the
file control available under HTML controls. and set the runat property of
that control to server.
In your button event use the following statement to get the uploaded file
details
(assume the control is named as fileCntrl)

fileCntrl.PostedFile.FileName -- gives you the selected file name
*place a image control in your page and assign this filename to the
src
property of the control at run time, to show the image before
uploading

fileCntrl.PostedFile.FileStream -- gives you the stream obj ref, so u can
read the contents of the file, and store it into a DB or folder, whatever way
you want.


Regards,
:) Thangam
 
S

sameer

Hey Gold, i got stuck again and this is where it is

when i click on the file control browse and select an image, i want the user
to see the image in the image Box but the file control does not have any
event at the server which fires where i select an image in it, any ideas how
i can do this.?
 
G

Gold

Hi,

there is no direct way to do this, still you can get the same behaviour
using client side scripts. The file control raises a client side event
whenever there is a change in any of the property, so when a file is
selected, the selected filename is assigned to the value property of the
control, which automatically causes the event to fire. Within this event
routine (written in javascript) you can get the file name and assign it to
the image control. One nice thing is that, everything is performed without a
postback....Try this

:)
Thangam
 
G

Gold

Hey, I am sorry I forget to tell you the name of the event it's

onpropertychange

so, your code should look like the following

<input type="file" id="selectfile" onpropertychange="displayimage();"
runat="server">

//Thangam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top