There are two ways to save images on the server. One is to save the images in the database, and the other is to save the images in the form of files under a directory on the website, however, this directory has write permissions for web users and is saved in binary format in the database. It is read as a file stream. If the WinForm program is very popular during development, however, it is better to save the file name under the Web folder and store it in the database.
Below is a piece of code that is stored in the data in a two-character format.
Code
Protected void button#click (object sender, EventArgs e)
{
If (FileUpload1.HasFile)
{
Byte [] content = FileUpload1.FileBytes;
SqlConnection Connection = new SqlConnection (@ "Data Source = COMPUTER \ SQLEXPRESS; Initial Catalog = test; Persist Security Info = True; User ID = sa; Password = sa ");
Connection. Open ();
SqlCommand Command = Connection. CreateCommand ();
Command. CommandText = "insert into images (image, contentType) values (@ image, @ contentType )";
Command. CommandType = CommandType. Text;
Command. Parameters. Add ("@ image", SqlDbType. Image). Value = content;
Command. Parameters. Add ("@ contentType", SqlDbType. NVarChar). Value = GetContentType (new FileInfo (FileUpload1.FileName). Extension. Remove (0, 1 ));
If (Command. ExecuteNonQuery () = 1)
{
Response. Write ("<script type = 'text/javascript '> alert ('added successfully'); </script> ");
}
}
}
Private string GetContentType (string extension)
{
String type = "jpeg ";
If (extension = "jpg ")
{
Type = "jpeg ";
}
Else
{
Type = extension;
}
Return "image/" + type;
}
In this way, it is saved in the form of two characters, and a piece of read Code is pasted below. However, this Code can only read one value Code.
Try
{
SqlConnection connection = new SqlConnection (@ "Data Source = COMPUTER \ SQLEXPRESS; Initial Catalog = test; Persist Security Info = True; User ID = sa; Password = sa ");
Connection. Open ();
SqlCommand command = connection. CreateCommand ();
Command. CommandText = "select * from images id = @ id"; // specify the parameter without id
Command. CommandType = CommandType. Text;
SqlDataReader reader = command. ExecuteReader ();
While (reader. Read ())
{
Response. ContentType = reader ["contentType"]. ToString ();
Response. BinaryWrite (byte []) reader ["image"]);
}
Response. End ();
Connection. Close ();
}
Catch
{
Response. End ();
}
}
The above code is estimated to be of little practical use, so it is not clearly written.
The following code shows how to add, save, and browse images on a page. The Code goes first.
Code
String strconn = @ "Data Source = COMPUTER \ SQLEXPRESS; Initial Catalog = test; User ID = sa; Password = sa ";
Protected void Page_Load (object sender, EventArgs e)
{
Using (SqlConnection Connection = new SqlConnection (strconn ))
{
SqlCommand Command = Connection. CreateCommand ();
Command. CommandText = "select * from images ";
Command. CommandType = CommandType. Text;
Connection. Open ();
SqlDataReader reader = Command. ExecuteReader ();
DataList1.DataSource = reader;
DataList1.DataBind ();
}
}
Protected void button#click (object sender, EventArgs e)
{
If (FileUpload1.HasFile)
{
String fileName = DateTime. Now. ToString ("yyyyMMddHHmmss"); // format the time
String extension = new FileInfo (FileUpload1.FileName). Extension;
FileName = fileName + extension;
Using (SqlConnection Connection = new SqlConnection (strconn ))
{
SqlCommand Command = Connection. CreateCommand ();
Command. CommandText = "insert into images (filename, description, datetime) values (@ filename, @ description, '" + DateTime. Now + "')";
Command. CommandType = CommandType. Text;
Command. Parameters. Add ("@ filename", SqlDbType. NChar). Value = fileName;
Command. Parameters. Add ("@ description", SqlDbType. NChar). Value = TextBox1.Text;
Try
{
Connection. Open ();
If (Command. ExecuteNonQuery () = 1)
{
FileUpload1.SaveAs (MapPath ("~ /Upload/") + fileName );
Label1.Text = "added successfully ";
}
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}
}
}
}
DataList control is used to display the image. The image binding code is used here.
Code
<Div id = "show">
<Asp: DataList ID = "DataList1" runat = "server" RepeatDirection = "Horizontal"
RepeatColumns = "3">
<ItemTemplate>
<Asp: Image ID = "Image1" runat = "server" ImageUrl = '<% # (Request. applicationPath + ("/upload/") + Eval ("filename ")). trim () %>'
Width = "100px" Height = "100px"/>
<Br/>
<Asp: Label ID = "Label2" runat = "server" Text = '<% # Eval ("description") %>'> </asp: Label>
</ItemTemplate>
</Asp: DataList>
</Div>
We can see that this Code does not have ImageUrl = '<% # (Request. ApplicationPath + ("/upload/") + Eval ("filename"). Trim () %>'
The address used to read the image. The image name is named after the time.
The next figure
/Files/izxp/uploadimages.rar