Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

how to upload a JPG image and save it on SQL Server or servers hard Drive

0.00/5 (No votes)
29 Jan 2010 1  
In this article you will learn how to upload a JPG image and save it on SQL Server or servers hard Drive.To get started create a blank asp.net page, and add a File Upload Control from Standard tab of visual studio toolbar or add the following code to the section you have planned to add the...
In this article you will learn how to upload a JPG image and save it on SQL Server or servers hard Drive.
To get started create a blank asp.net page, and add a File Upload Control from Standard tab of visual studio toolbar or add the following code to the section you have planned to add the upload control.
    <br />
<asp:fileupload id="PicUpload" runat="server" /><br />

This adds a file browser control to your webpage, which you can select one file at a time from your local hard drive. Switch to code view and add the following code on top of your page, where other name spaces are imported.
<br />
Using Sytem.IO;<br />

switch back to design view, add button or image button to your page (name it Upload for example) and add the following code the buttons Click Event:
if (PicUpload.PostedFile != null)
        {
         HttpPostedFile Pic = PicUpload.PostedFile;
            if (Pic.ContentLength == 0)
            {
                Response.Write("فایل شما هیچ محتوایی ندارد!");
                return;
            }
        if (Path.GetExtension(Pic.FileName).ToLower() != ".jpg")
            {
                Response.Write("پسوند این فایل نامعتبر است.");
                return;
            }
        byte[] data = new Byte[Pic.ContentLength];
        Pic.InputStream.Read(data, 0, Pic.ContentLength);
        string SavePath = @"images/";
        newPic = new FileStream(Server.MapPath(SavePath + Pic.FileName), FileMode.Create);
        newPic.Write(data, 0, Pic.ContentLength);
        newPic.Close();
        }else
        {
            Response.Write("Please Select a File! ");
        }

In first line of the code we check if user has selected a file or not, if not the code will return an error message. Then we create a HttpPostedFile variable, called Pic, then we we verify the file length and extension( in this case we allow users to upload .JPG files only) This Lines read selected file in to a stream and makes it ready for saving :
<br />
byte[] data = new Byte[Pic.ContentLength];<br />
Pic.InputStream.Read(data, 0, Pic.ContentLength);<br />

now that we have the file in a stream we can save it by following code :
<br />
FileStream newPic = new FileStream(Server.MapPath(SavePath + Pic.FileName), FileMode.Create);<br />
newPic.Write(data, 0, Pic.ContentLength);<br />

You can use code bellow instead of above two lines to save your image in a SQL Server data base :
object objImage = data;
SqlConnection connection = new SqlConnection(connectionString);
string sql_insert = "INSERT INTO tblImage (picture) values (@img)";
SqlCommand command = new SqlCommand(sql_insert, connection);
SqlParameter parameter = new SqlParameter("@img", SqlDbType.Image);
parameter.Value = objImage;
command.Parameters.Add(parameter);
connection.Open();
command.ExecuteNonQuery();
connection.Close();

All you have to do is change your connection string and rename your database table name and column in sql_insert variable.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here