C # Implementation of upload photos to the physical path, and save the address to the database of a small example _ practical tips

Source: Internet
Author: User

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>
&LT;TD colspan= "2" style= "height:21px" >

</td>
</tr>
<tr>
&LT;TD style= "width:400px" >
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<asp:label id= "Label1" runat= "Server" forecolor= "Red" ></asp:Label>
</td>
&LT;TD style= "width:80px" >
<asp:button id= "Uploadbutton" runat= "server" text= "Upload picture" onclick= "Uploadbutton_click"/>
</td>
</tr>
<tr>
&LT;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);
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.