C # example of how to upload a photo to a physical path and save the address to a database

Source: Internet
Author: User

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 );
}
}
}
}

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.