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 ?

Comments

  • : 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 ?
    :
    :

    First off, if the images need to be displayed on the website, then it will be a bad idea to store the image in the database.

    To set up a page for uploading u have to add a file input tag and set the form tags enctype attribute like so.

    [code]




    [/code]

    The "multipart/form-data" causes the web browser to upload any object under a general binary format.

    On the code side, you execute the upload logic in the upload buttons click event. In my example "btnUpload" accomplishes this.

    Below is an actual sample that I have pulled from one of my projects. The code is reformatted to fit this screen. Plus I commented it so that u understand.

    [code]
    Private Sub btnUpload_Click(ByVal s As Object, ByVal e As EventArgs) _
    Handles btnUpload.Click

    ' THIS LINE GENERATES A RANDOM FILE-NAME
    Dim file As String
    file = System.Environment.TickCount.ToString & ".jpg"

    Call CleanFolder(mTemp)

    ' THE HTTPPostedFile object, which is part of the HtmlInputFile ...
    ' object encapsulates all the upload functionality. U CAN ...
    ' QUERY THE HTTPPostedFile object via the PostedFile property.
    With filImageUpload.PostedFile

    ' HERE I AM LEVERAGING THE ContentType Property to filter ...
    ' out all other files accept *.gif and *.jpg.
    ' NOTE:
    ' "image/pjpeg" is the content type that Internet Explorer ...
    ' returns for jpg images.

    Select Case .ContentType.ToLower
    Case "image/gif", "image/pjpeg", "image/jpeg"

    ' HERE I AM CREATING GDI+ IMAGE OBJECT. This ...
    ' is accomplished by using a stream object ...
    ' created via the HTTPPostedFile .InputStream property.

    Dim img As gdi.Image
    img = gdi.Image.FromStream(.InputStream)

    ' HERE I AM THUMBNAILING THE IMAGE
    img = img.GetThumbnailImage( _
    img.Width 5, _
    img.Height 5, _
    Nothing, Nothing)

    ' SAVE IMAGE ON SERVER AS *.jpg NO MATTER WHAT
    img.Save( _
    IO.Path.Combine(mTemp, file), _
    Imaging.ImageFormat.Jpeg)

    ' CLEAN-UP IMAGE OBJECT
    img.Dispose()

    ViewState("Temp") = file

    lblImage.Visible = False
    imgImage.ImageUrl = TEMP_FOLDER & file & "?" & _
    System.Environment.TickCount

    imgImage.Visible = True

    Case Else
    ' TODO: PLACE ERROR HERE

    End Select
    End With

    End Sub
    [/code]

    This example code assumes theimports statement below is being used:

    [code]
    imports gdi = System.Drawing
    [/code]

    My code example simply creates an image object from the upload stream, filters out all files accept *.gif and *.jpg files, thumbnails the image and saves it on the server.

    If you do not feel like mnipulating the image before it is saved, you can just use the HTTPPOstedFile objects "SaveAs" function instead of creating an image object.

    [code]
    ...
    filImageUpload.SaveAs(Server.MapPath("UploadFolder") & "" & file)
    ...
    [/code]



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion