C # Implements a small example of uploading a photo to a physical path and saving the address to a database
Effect:
Ideas:
First, get the physical address of the picture, then make a decision to save the picture to a folder, and then save the picture information to the database.
Database:
CREATE TABLE Image1 ( ID int identity (primary) key, ImageName varchar (+), ImageType varchar (20), ImagePath varchar ($) )
Code:
<body> <form id= "Form1" runat= "Server" > <p> <table> <tr> <TD colspan= "2" style= "height:21px" > </td> </tr> <tr> <td style= "width:400px" > <ASP:FILEUPL Oad id= "FileUpload1" runat= "server"/> <asp:label id= "Label1" runat= "Server" forecolor= "Red" &G t;</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= "118 PX "width=" 131px "/> </td> </tr> </table> </p> </form> </bod Y>
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 (Objec T sender, EventArgs e) {} string SQLString = configurationmanager.connectionstrings["ConnectionStr"] . ToString (); protected void Uploadbutton_click (object sender, EventArgs e) {try {using (S Qlconnection Sqlcon = new SqlConnection (SQLString)) {String FullName = Fileupload1.post edfile.filename;//get the picture physical address FileInfo fi = new FileInfo (FullName); String name = Fi. name;//Gets the picture name string type = Fi. extension;//gets the 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,imag Etype,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); } } }}
The above is the C # implementation of uploading photos to the physical path, and save the address to the database of small examples of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!