Effect:
Ideas:
First, get the physical address of the picture, then make a decision to save the picture to the folder, and then save the picture information to the database.
Database:
Copy Code code as follows:
CREATE TABLE Image1
(
ID int Identity (1,1) primary key,
imagename varchar (100),
ImageType varchar (20),
ImagePath varchar (200)
)
Code:
Copy Code code 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 picture" onclick= "Uploadbutton_click"/>
</td>
</tr>
<tr>
<TD colspan= "2" align= "Center" >
<br/>
<br/>
<asp:image id= "Image1" runat= "Server" height= "118px" width= "131px"/>
</td>
</tr>
</table>
</div>
</form>
</body>
Copy Code code 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;//Get Picture Physical Address
FileInfo fi = new FileInfo (FullName);
String name = Fi. name;//Get Picture Name
String type = fi. extension;//Get Picture Type
if (type = = ". jpg" | | | type = = ". gif" | | type = = ". bmp" | | type = = ". png")
{
String savepath = Server.MapPath ("~\\excel");//Picture saved to Folder
This. FileUpload1.PostedFile.SaveAs (Savepath + "\" + name);/save Path
This. Image1.visible = true;
This. Image1.imageurl = "~\\excel" + "\" + name;//interface Display picture
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 = "Upload success";
}
Else
{
This.label1.Text = "Please select the correct format picture";
}
}
}
catch (Exception ex)
{
Response.Write (ex. message);
}
}
}
}