C # Implements a small example of uploading a photo to a physical path and saving the address to a database

Source: Internet
Author: User
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>              &LT;TD colspan= "2" style= "height:21px" > </td> </tr> <tr> <td style= "width:400px" > &LT;ASP:FILEUPL Oad id= "FileUpload1" runat= "server"/> <asp:label id= "Label1" runat= "Server" forecolor= "Red" &G t;</asp:label> </td> <td style= "width:80px" > &lt              ; 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)!

  • Related Article

    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.