zoukankan      html  css  js  c++  java
  • 点滴积累【C#】---C#实现上传照片到物理路径,并且将地址保存到数据库,

    效果:

    思路:

    首先,获取图片物理地址,然后进行判断将图片保存到文件夹下,再将图片的信息保存到数据库。

    数据库:

    1 create table image1
    2 (
    3 ID int identity(1,1) primary key,
    4 ImageName varchar(100) ,
    5 ImageType varchar(20),
    6 ImagePath varchar(200)
    7 )

    代码:

     1 <body>
     2     <form id="form1" runat="server">
     3     <div>
     4         <table>
     5             <tr>
     6                 <td colspan="2" style="height: 21px">
     7                     &nbsp;
     8                 </td>
     9             </tr>
    10             <tr>
    11                 <td style=" 400px">
    12                     <asp:FileUpload ID="FileUpload1" runat="server" />
    13                     &nbsp;<asp:Label ID="label1" runat="server" ForeColor="Red"></asp:Label>
    14                 </td>
    15                 <td style=" 80px">
    16                     <asp:Button ID="UploadButton" runat="server" Text="上传图片" OnClick="UploadButton_Click" />
    17                 </td>
    18             </tr>
    19             <tr>
    20                 <td colspan="2" align="center">
    21                     <br />
    22                     <br />
    23                     <asp:Image ID="Image1" runat="server" Height="118px" Width="131px" />
    24                 </td>
    25             </tr>
    26         </table>
    27     </div>
    28     </form>
    29 </body>
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 using System.IO;
     8 using System.Configuration;
     9 using System.Data;
    10 using System.Data.SqlClient;
    11 
    12 namespace InExcelOutExcel
    13 {
    14     public partial class UpWord : System.Web.UI.Page
    15     {
    16         protected void Page_Load(object sender, EventArgs e)
    17         {
    18 
    19         }
    20         string SQLString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();
    21         protected void UploadButton_Click(object sender, EventArgs e)
    22         {
    23             try
    24             {
    25                 using (SqlConnection sqlcon = new SqlConnection(SQLString))
    26                 {
    27                     string FullName = FileUpload1.PostedFile.FileName;//获取图片物理地址
    28                     FileInfo fi = new FileInfo(FullName);
    29                     string name = fi.Name;//获取图片名称
    30                     string type = fi.Extension;//获取图片类型
    31                     if (type == ".jpg" || type == ".gif" || type == ".bmp" || type == ".png")
    32                     {
    33                         string SavePath = Server.MapPath("~\excel");//图片保存到文件夹下
    34                         this.FileUpload1.PostedFile.SaveAs(SavePath + "\" + name);//保存路径
    35                         this.Image1.Visible = true;
    36                         this.Image1.ImageUrl = "~\excel" + "\" + name;//界面显示图片
    37                         string sql = "insert into image1(ImageName,ImageType,ImagePath) values('" + name + "','" + type + "','~\excel" + name + "')";
    38                         SqlCommand cmd = new SqlCommand(sql, sqlcon);
    39                         sqlcon.Open();
    40                         cmd.ExecuteNonQuery();
    41                         this.label1.Text = "上传成功";
    42                     }
    43                     else
    44                     {
    45                         this.label1.Text = "请选择正确的格式图片";
    46                     }
    47                 }
    48             }
    49             catch (Exception ex)
    50             {
    51                 Response.Write(ex.Message);
    52             }
    53         }
    54     }
    55 }
  • 相关阅读:
    Windows Azure 架构指南 – 第 1卷 发布
    SQL Azure 入门教学(一):SQL Azure之初体验
    WPC大会新动态: Windows Azure Platform Appliance发布
    Windows Azure AppFabric 入门教学(七):多播(Multicast)
    PHP on Windows Azure 入门教学系列(一):在Windows Azure内运行PHP应用
    SQL Azure SU3 现已在全球6座数据中心开始启用
    WPC大会新动态:合作伙伴采纳Windows Azure
    时间的运算
    把字符串复制到剪贴板
    常用的表格效果
  • 原文地址:https://www.cnblogs.com/xinchun/p/3485643.html
Copyright © 2011-2022 走看看