Effect:
Ideas:
First, obtain the physical address of the image, then judge to save the image to a folder, and then save the image information to the database.
Database:
Copy codeThe Code is as follows:
Create table image1
(
ID int identity (1, 1) primary key,
Imageename varchar (100 ),
ImageType varchar (20 ),
ImagePath varchar (200)
)
Code:
Copy codeThe Code is as follows:
<Body>
<Form id = "form1" runat = "server">
<Div>
<Table>
<Tr>
<Td colspan = "2" style = "height: 21px">
</Td>
</Tr>
<Tr>
<Td style = "width: 400px">
<Asp: FileUpload ID = "FileUpload1" runat = "server"/>
<Asp: Label ID = "label1" runat = "server" ForeColor = "Red"> </asp: Label>
</Td>
<Td style = "width: 80px">
<Asp: Button ID = "UploadButton" runat = "server" Text = "Upload image" OnClick = "UploadButton_Click"/>
</Td>
</Tr>
<Tr>
<Td colspan = "2" align = "center">
<Br/>
<Br/>
<Asp: Image ID = "Image1" runat = "server" Height = "118px" Width = "pixel PX"/>
</Td>
</Tr>
</Table>
</Div>
</Form>
</Body>
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. IO;
Using System. Configuration;
Using System. Data;
Using System. Data. SqlClient;
Namespace InExcelOutExcel
{
Public partial class UpWord: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
String SQLString = ConfigurationManager. ConnectionStrings ["ConnectionStr"]. ToString ();
Protected void UploadButton_Click (object sender, EventArgs e)
{
Try
{
Using (SqlConnection sqlcon = new SqlConnection (SQLString ))
{
String FullName = FileUpload1.PostedFile. FileName; // obtain the physical address of the image.
FileInfo fi = new FileInfo (FullName );
String name = fi. Name; // obtain the image name
String type = fi. Extension; // obtain the image type
If (type = ". jpg" | type = ". gif" | type = ". bmp" | type = ". png ")
{
String SavePath = Server. MapPath ("~ \ Excel "); // Save the image to a folder
This. FileUpload1.PostedFile. SaveAs (SavePath + "\" + name); // save path
This. Image1.Visible = true;
This. Image1.ImageUrl = "~ \ Excel "+" \ "+ name; // The image is displayed on the page.
String SQL = "insert into image1 (ImageName, ImageType, ImagePath) values ('" + name + "', '" + type + "','~ \ Excel "+ name + "')";
SqlCommand cmd = new SqlCommand (SQL, sqlcon );
Sqlcon. Open ();
Cmd. ExecuteNonQuery ();
This. label1.Text = "uploaded successfully ";
}
Else
{
This. label1.Text = "Please select the correct format image ";
}
}
}
Catch (Exception ex)
{
Response. Write (ex. Message );
}
}
}
}