Forum
 
ForumForumDiscussions and...Discussions and....Net framework ....Net framework ...HowTo: Upload an image direct from the browser to the databaseHowTo: Upload an image direct from the browser to the database
Previous Previous
 
Next Next
New Post
 17/03/2008 08:29
 
 Modified By RichardHowells  on 17/03/2008 09:32:34

This code snippet shows how to upload an image from the browser using the FileUpload control and save it directly into the database without it ever needing to touch the HDD on the server.  The sample code is for SQL Server.  This example uses the Northwind database. 

Be aware that the images in the Microsoft released version of Northwind all have an unusual 78 byte header on them.  This code does not create such a header and so may be incompatible with other code that processes Northwind images. 

        SqlConnection cn = new SqlConnection(@"Data Source=.;
                Initial Catalog=Northwind;Integrated Security=True");
        cn.Open();
        using (cn)
        {
            using (SqlCommand cm = new SqlCommand(@"update employees
                    set photo = @photo where EmployeeId = 1", cn))
            {
                SqlBytes theImage = new SqlBytes(FileUpload1.FileContent);
                cm.Parameters.AddWithValue("@photo", theImage);
                int rows = cm.ExecuteNonQuery();
            }
        }

 

This code uses the FileContent property of the file upload control to access the uploaded data as a stream.  The SQL Server specific data type SqlBytes acts as an intermediary between the uploaded stream from the browser and the Photo column (type image) in the database.

Security

Note that this code blindly accepts whatever the browser uploads without even making an attempt to check the mime type. Depending on what happens to the uploaded file later this might be a risky process.


Cheers,
- Richard
If this post helped you over a problem, or taught you something new, please login and rate it. Ratings are in the drop down in the top left corner
Previous Previous
 
Next Next
ForumForumDiscussions and...Discussions and....Net framework ....Net framework ...HowTo: Upload an image direct from the browser to the databaseHowTo: Upload an image direct from the browser to the database

Forum Usage Guidelines

The forums are a place for all to exchange ideas and techniques, and to post and answer questions.  All are welcome to read, registration is required to post. 

If you learn somthing new, discover or acquire a new technique, then please take a moment to register and rate the post that just helped you.  This site does not send spam and it does not release your personal details.  Full details in the site privacy policy.

We have some simple posting guidelines to keep the forums a pleasant and informative environment.

  • No flames, no trolls
  • No profanity, no racism
  • Site management has the final word on approving or removing any thread, post, or comment
  • English language only please

 

Copyright 2002-15 by Dynamisys Ltd