当前位置: 移动技术网 > IT编程>开发语言>.net > C#实现上传照片到物理路径,并且将地址保存到数据库的小例子

C#实现上传照片到物理路径,并且将地址保存到数据库的小例子

2017年12月12日  | 移动技术网IT编程  | 我要评论

天刹星魔,黑鳍的赃物,程立人

效果:

思路:

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

数据库:

复制代码 代码如下:

create table image1
(
id int identity(1,1) primary key,
imagename varchar(100) ,
imagetype varchar(20),
imagepath varchar(200)
)

代码:

复制代码 代码如下:

<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="上传图片" onclick="uploadbutton_click" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <br />
                    <br />
                    <asp:image id="image1" runat="server" height="118px" width="131px" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>

复制代码 代码如下:

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;//获取图片物理地址
                    fileinfo fi = new fileinfo(fullname);
                    string name = fi.name;//获取图片名称
                    string type = fi.extension;//获取图片类型
                    if (type == ".jpg" || type == ".gif" || type == ".bmp" || type == ".png")
                    {
                        string savepath = server.mappath("~\\excel");//图片保存到文件夹下
                        this.fileupload1.postedfile.saveas(savepath + "\\" + name);//保存路径
                        this.image1.visible = true;
                        this.image1.imageurl = "~\\excel" + "\\" + name;//界面显示图片
                        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 = "上传成功";
                    }
                    else
                    {
                        this.label1.text = "请选择正确的格式图片";
                    }
                }
            }
            catch (exception ex)
            {
                response.write(ex.message);
            }
        }
    }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网